473,231 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 18769
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Kris | last post by:
Hi! I have the following instruction: char wyp; scanf("%c",&wyp); this scanf is in a while loop, and I want it to get EXACTLY one character each time. Does anyone knows how to do it? BTW....
39
by: Teh Charleh | last post by:
OK I have 2 similar programmes, why does the first one work and the second does not? Basically the problem is that the program seems to ignore the gets call if it comes after a scanf call. Please...
7
by: Przemo Drochomirecki | last post by:
hi, SCANF is not well described in my books, i'd like to do this stuff: 1) read all numbers from a line, separated by #32 2) read all numbers from a line, separaed by ',' 3) read whole line ...
7
by: happy | last post by:
I need to write a string field in a structure data type to a file using fprintf,fscanf. I failed to use the below syntax. Please I need to not go outside scanf,fprintf,printf . /* Book name ...
51
by: moosdau | last post by:
my code: do { printf("please input the dividend and the divisor.\n"); if(!scanf("%d%d",&dend,&dor)) { temp1=1; fflush(stdin); } else
27
by: guoliang | last post by:
help: #include<stdio.h> int main(void) { int a; char b; printf("int=");
3
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
6
by: amarapreet | last post by:
hi gcc 3.4 says in a warning that %lf is not recognized as a scanf format specifier is this right if so how to read in a double using scanf thanks
51
by: deepak | last post by:
Hi, For the program pasted below, scanf is not waiting for the second user input. Can someone suggest reason for this? void main() { char c;
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.