469,632 Members | 1,618 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

sscanf regex

say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store
it into another array.

so:

char buf[] = "string1 string2 string3";
char array[100];

sscanf(buf, "what format string should we use?", array);
Nov 14 '05 #1
10 7071
j0mbolar wrote:
say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store
it into another array.

so:

char buf[] = "string1 string2 string3";
char array[100];

sscanf(buf, "what format string should we use?", array);


If I understand your intent correctly (not certain):

if (sscanf(buf, "%*s%*s%99s", array) == 1)
...

is what you want. This format

- Skips any leading white space, gathers the
longest possible batch of non-white characters,
and doesn't store them anywhere, then

- Skips any more white space, gathers another batch
of non-white characters, and doesn't store them
anywhere, then

- Skips any more white space, gathers a third batch
of non-white characters (but no more than 99 of
them), stores them in `array', and returns 1 to
indicate that it has converted and stored one
field.

If the scan reaches the end of `buf' before finding all
three non-white strings, sscanf() returns either zero or
EOF depending on the exact circumstances.

--
Er*********@sun.com

Nov 14 '05 #2

On Wed, 16 Jun 2004, j0mbolar wrote:

say you have char buf[] = "string1 string2 string3";
then you want to use sscanf to match "string3" and store
it into another array.

char buf[] = "string1 string2 string3";
char array[100];

sscanf(buf, "what format string should we use?", array);


sscanf(buf, "%*s%*s%s", array);

would be the simplest, I think.

-Arthur
Nov 14 '05 #3
j0******@engineer.com (j0mbolar) wrote:
say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store
it into another array.

so:

char buf[] = "string1 string2 string3";
char array[100];

sscanf(buf, "what format string should we use?", array);


sscanf( buf, "%*[^ ] %*[^ ] %s", array );
Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #4
In <2d**************************@posting.google.com > j0******@engineer.com (j0mbolar) writes:
say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store
it into another array.

so:

char buf[] = "string1 string2 string3";
char array[100];

sscanf(buf, "what format string should we use?", array);


You don't want to use sscanf for this purpose.

#define TARGET "string3"
char *p;

if ((p = strstr(buf, TARGET)) != NULL) {
memcpy(array, p, strlen(TARGET));
array[strlen(TARGET)] = 0;
}

Of course, for this trivial example, you don't need to extract the
pattern from the input string, so

if (strstr(buf, TARGET) != NULL) strcpy(array, TARGET);

will do.

The closest you can get in the way of regex support in standard C are
strstr(), str[c]spn(), strpbrk() and the %[ conversion specifier of
sscanf.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #5
In <40**************@sun.com> Eric Sosman <Er*********@sun.com> writes:
j0mbolar wrote:
say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store ^^^^^^^^ it into another array.

so:

char buf[] = "string1 string2 string3";
char array[100];

sscanf(buf, "what format string should we use?", array);


If I understand your intent correctly (not certain):

if (sscanf(buf, "%*s%*s%99s", array) == 1)
...

is what you want. This format


Could you please explain how is this code checking the presence of
"string3" in buf? I suppose this what the OP meant by "to match".

Unless I'm missing something obvious, the OP wasn't asking about how to
extract the third word in buf.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #6
Dan Pop wrote:
In <40**************@sun.com> Eric Sosman <Er*********@sun.com> writes:

j0mbolar wrote:
say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store
^^^^^^^^
it into another array.

so:

char buf[] = "string1 string2 string3";
char array[100];

sscanf(buf, "what format string should we use?", array);


If I understand your intent correctly (not certain):

if (sscanf(buf, "%*s%*s%99s", array) == 1)
...

is what you want. This format

Could you please explain how is this code checking the presence of
"string3" in buf? I suppose this what the OP meant by "to match".

Unless I'm missing something obvious, the OP wasn't asking about how to
extract the third word in buf.


Well, I *said* I wasn't sure I'd understood the O.P.'s
intent. I thought about offering

if (strchr(buf, "string3") != NULL)
strcpy(array, "string3");

.... but that seemed pointless, so I tried to look for another
interpretation of an imprecise problem statement. And then I
told him what my "solution" did, just in case I was solving
the wrong problem.

Now I'll get back to devising the circuitry behind the
DWIM instruction in the next-generation SPARC ;-)

--
Er*********@sun.com

Nov 14 '05 #7
Eric Sosman <Er*********@sun.com> wrote:
Dan Pop wrote:
[snip]
Could you please explain how is this code checking the presence of
"string3" in buf? I suppose this what the OP meant by "to match".

Unless I'm missing something obvious, the OP wasn't asking about how to
extract the third word in buf.

Well, I *said* I wasn't sure I'd understood the O.P.'s
intent. I thought about offering if (strchr(buf, "string3") != NULL) ^^^^^^ strcpy(array, "string3");


I think you mean 'strstr'.

--
Alex Monjushko (mo*******@hotmail.com)
Nov 14 '05 #8
Da*****@cern.ch (Dan Pop) wrote:
In <40**************@sun.com> Eric Sosman <Er*********@sun.com> writes:
j0mbolar wrote:
say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store ^^^^^^^^ it into another array.
.... If I understand your intent correctly (not certain):

if (sscanf(buf, "%*s%*s%99s", array) == 1)
...
Could you please explain how is this code checking the presence of
"string3" in buf? I suppose this what the OP meant by "to match".


Er... well, maybe, but it doesn't make much sense to look for a
literal occurrence of a certain substring *and then copy that to
another array*, does it?!?
Unless I'm missing something obvious, the OP wasn't asking about how to
extract the third word in buf.


As I understood, the OP was asking exactly about that: how to
extract the last of three blank separated words from a given
string, but used the word 'match' in a less strict sense than
you inferred. I may still be mistaken, though.

Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #9
In <40**************@sun.com> Eric Sosman <Er*********@sun.com> writes:
Dan Pop wrote:
In <40**************@sun.com> Eric Sosman <Er*********@sun.com> writes:

j0mbolar wrote:

say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store
^^^^^^^^
it into another array.

so:

char buf[] = "string1 string2 string3";
char array[100];

sscanf(buf, "what format string should we use?", array);

If I understand your intent correctly (not certain):

if (sscanf(buf, "%*s%*s%99s", array) == 1)
...

is what you want. This format

Could you please explain how is this code checking the presence of
"string3" in buf? I suppose this what the OP meant by "to match".

Unless I'm missing something obvious, the OP wasn't asking about how to
extract the third word in buf.


Well, I *said* I wasn't sure I'd understood the O.P.'s
intent. I thought about offering

if (strchr(buf, "string3") != NULL)
strcpy(array, "string3");

... but that seemed pointless,


It was far less pointless than your "solution", because it was
actually doing the pattern matching required by the OP and *conditionally*
storing the matched pattern into array.
so I tried to look for another
interpretation of an imprecise problem statement.


1. The problem statement was as precise as you can get. It's just that
sscanf can't be used as part of the solution.

2. Interpreting a request for pattern matching as a request for finding
the third word in a string doesn't make any sense to me.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #10
In <63********************************@4ax.com> Irrwahn Grausewitz <ir*******@freenet.de> writes:
Da*****@cern.ch (Dan Pop) wrote:
In <40**************@sun.com> Eric Sosman <Er*********@sun.com> writes:
j0mbolar wrote:
say you have char buf[] = "string1 string2 string3";

then you want to use sscanf to match "string3" and store

^^^^^^^^
it into another array.... If I understand your intent correctly (not certain):

if (sscanf(buf, "%*s%*s%99s", array) == 1)
...


Could you please explain how is this code checking the presence of
"string3" in buf? I suppose this what the OP meant by "to match".


Er... well, maybe, but it doesn't make much sense to look for a
literal occurrence of a certain substring *and then copy that to
another array*, does it?!?


Not as a *complete* programming problem, but it is perfectly sensible
as *part* of a programming problem, where the contents of the other
array may depend on a sequence of tests:

- if the pattern match succeeds, it is the pattern
- else if ..., it is ...
- else it is a default value
Unless I'm missing something obvious, the OP wasn't asking about how to
extract the third word in buf.


As I understood, the OP was asking exactly about that: how to
extract the last of three blank separated words from a given
string, but used the word 'match' in a less strict sense than
you inferred. I may still be mistaken, though.


The usage of the word "regex" in the subject line is a very strong
clue that he used the word "match" as in a regular expression context.

Otherwise, the straightforward problem description would have been:
"how can I use sscanf to extract the third word in a string?", but
I strongly suspect that the OP could figure out the answer by himself.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Da Wang | last post: by
2 posts views Thread by Mark Oliver | last post: by
3 posts views Thread by Evan Mathias | last post: by
4 posts views Thread by sklett | last post: by
3 posts views Thread by ewolfman | last post: by
20 posts views Thread by AMP | last post: by
5 posts views Thread by Alex Mathieu | last post: by
9 posts views Thread by Bint | last post: by
9 posts views Thread by Tim Streater | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.