scanf and regular expressions

This is a discussion on scanf and regular expressions within the C Programming forums, part of the General Programming Boards category; Hi, I have the following statement to my code: sscanf(line, "%[^=\n]=%[^\n]", tag, value); which if I understand well it means ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21