468,497 Members | 1,869 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,497 developers. It's quick & easy.

scanf %[] information

Hi.

I'm trying to find some tutorial-type information on using scanf's
regular-expression subset for parsing - to no avail so far.

I've looked through quite a few posts to clc, but none that have helped
me understand what's really allowed, also, the c-faq - when searched
for scanf regular (using google site-search) didn't help.

Does anyone know of some source - preferably web-based with examples -
that could help me understand what's allowed (e.g., subset of normal
regular-expression syntax is valid)?

Thanks

danny

Aug 8 '06 #1
3 18213
so**********@gmail.com wrote:
Hi.

I'm trying to find some tutorial-type information on using scanf's
regular-expression subset for parsing - to no avail so far.

I've looked through quite a few posts to clc, but none that have helped
me understand what's really allowed, also, the c-faq - when searched
for scanf regular (using google site-search) didn't help.

Does anyone know of some source - preferably web-based with examples -
that could help me understand what's allowed (e.g., subset of normal
regular-expression syntax is valid)?
Ok, so I've tried the following:

char s1[10] = { '\0' }, s2[10] = { '\0' };

scanf("%9[^abc]%9s", s1, s2);

printf("s1 = %s s2 = %s\n", s1, s2);

My understanding of this is that the scanf line works like this -

%9[^abc] tells scanf to put characters NOT matching a or b or c into s1
- 9 specifies the max number of chars.

The following %9s uses s2 to 'catch' other characters.

If I input the following:

defabc

the output is s1 = def s2 = abc

However, if I input

abcdef

the output is s1 = s2 =

I thought/expected s1 to contain def and s2 to contain nothing.

Help appreciated!

danny

Aug 8 '06 #2
On Tue, 8 Aug 2006, so**********@gmail.com wrote:
[snip]

Ok, so I've tried the following:

char s1[10] = { '\0' }, s2[10] = { '\0' };

scanf("%9[^abc]%9s", s1, s2);

printf("s1 = %s s2 = %s\n", s1, s2);

My understanding of this is that the scanf line works like this -

%9[^abc] tells scanf to put characters NOT matching a or b or c into s1
- 9 specifies the max number of chars.
This is only half correct. What you have missed is that
the scanset applies sequentially, one-character-at-a-time (even
when the field width is 9).
The following %9s uses s2 to 'catch' other characters.

If I input the following:

defabc

the output is s1 = def s2 = abc

However, if I input

abcdef

the output is s1 = s2 =

I thought/expected s1 to contain def and s2 to contain nothing.
%9[^abc] accepts any characters except a, b or c. As the
first character is a, the input will not be accepted. Thus,
scanf should return 0 to indicate that nothing has been
successfully converted.

Tak-Shing
Aug 8 '06 #3
so**********@gmail.com wrote:
so**********@gmail.com wrote:
Hi.

I'm trying to find some tutorial-type information on using scanf's
regular-expression subset for parsing - to no avail so far.

I've looked through quite a few posts to clc, but none that have helped
me understand what's really allowed, also, the c-faq - when searched
for scanf regular (using google site-search) didn't help.

Does anyone know of some source - preferably web-based with examples -
that could help me understand what's allowed (e.g., subset of normal
regular-expression syntax is valid)?

Ok, so I've tried the following:

char s1[10] = { '\0' }, s2[10] = { '\0' };

scanf("%9[^abc]%9s", s1, s2);

printf("s1 = %s s2 = %s\n", s1, s2);

My understanding of this is that the scanf line works like this -

%9[^abc] tells scanf to put characters NOT matching a or b or c into s1
- 9 specifies the max number of chars.

The following %9s uses s2 to 'catch' other characters.

If I input the following:

defabc

the output is s1 = def s2 = abc

However, if I input

abcdef

the output is s1 = s2 =

I thought/expected s1 to contain def and s2 to contain nothing.

Help appreciated!
The first thing you need to understand is that the scanf functions do
you support/use regular expressions. Although the bracket syntax in
the format specifier operates in a similiar fashion to the bracketed
character class of a regular expression they not the same thing. The
format string "%9[^abc]%9s" means:

match one or more contigious (up to 9) characters that are not a, b, or
c and store the matched characters as a string into the area provided,
then skip any whitespace (the %s format specifier skips leading
whitespace) and match up to 9 contigious non-whitespace characters and
store them as a string into the second area provided.

Note that:
1. "[" format specifier must match at least one character for it to be
successful
2. The scanf functions process one character at a time, if at any time
the current character does not match the format specifier the match
fails and the function returns.

Failure can occur due to a matching failure or read error. The scanf
functions return the number of successful conversions performed so you
can check this and act accordingly. In your example, when you provide
the input "abcdef", scanf reads the first character, 'a', from the
stream, sees that it does not match the first format specifier, pushes
the character back onto the stream and returns. Remember that the "["
format specifier must match at least one character to be successful and
that once any conversion fails the function returns, it doesn't try to
match the input against the following format specifiers.

Robert Gamble

Aug 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Kris | last post: by
39 posts views Thread by Teh Charleh | last post: by
7 posts views Thread by Przemo Drochomirecki | last post: by
7 posts views Thread by happy | last post: by
27 posts views Thread by guoliang | last post: by
3 posts views Thread by Tinku | last post: by
6 posts views Thread by amarapreet | last post: by
51 posts views Thread by deepak | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.