Thread: scanf and regular expressions

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    28

    scanf and regular expressions

    Hi,

    I have the following statement to my code:

    sscanf(line, "%[^=\n]=%[^\n]", tag, value);

    which if I understand well it means scanf any character till a newline or an = character is encountered and store it to tag.

    If that is correct how can I add the white space character to be excluded as well. Or is there any way to truncate the white space at the end of tag since line is:

    name = Foo Bar\n\0

    and with the aboce sscanf tag is
    name \0
    there is a space before the \0.
    and value is
    Foo Bar\0

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    34,339
    sscanf(line, "%[^ =\n]%*[ =]%[^\n]", tag, value);
    The second being a sequence of spaces and =, with the * meaning the assignment is suppressed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    28
    Quote Originally Posted by Salem
    sscanf(line, "%[^ =\n]%*[ =]%[^\n]", tag, value);
    The second being a sequence of spaces and =, with the * meaning the assignment is suppressed.
    Thanks, that works just fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Whitespace and scanf()
    By Procyon in forum C Programming
    Replies: 1
    Last Post: 01-05-2002, 12:55 AM