sscanf regex

J

j0mbolar

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);
 
A

Arthur J. O'Dwyer

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
 
E

Eric Sosman

j0mbolar said:
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.
 
I

Irrwahn Grausewitz

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
 
D

Dan Pop

In said:
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
 
D

Dan Pop

In said:
j0mbolar said:
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
 
E

Eric Sosman

Dan said:
j0mbolar said:
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 ;-)
 
D

Dan Pop

In said:
Dan said:
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
 
I

Irrwahn Grausewitz

In said:
j0mbolar said:
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
 
D

Dan Pop

In said:
[email protected] (Dan Pop) said:
In said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top