Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sscanf regex

Reply
Thread Tools

sscanf regex

 
 
j0mbolar
Guest
Posts: n/a
 
      06-16-2004
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);
 
Reply With Quote
 
 
 
 
Arthur J. O'Dwyer
Guest
Posts: n/a
 
      06-16-2004

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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      06-16-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.
>
> 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      06-17-2004
(E-Mail Removed) (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 ((E-Mail Removed))
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
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-17-2004
In <(E-Mail Removed) > (E-Mail Removed) (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: (E-Mail Removed)
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-17-2004
In <(E-Mail Removed)> Eric Sosman <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      06-17-2004
Dan Pop wrote:
> In <(E-Mail Removed)> Eric Sosman <(E-Mail Removed)> 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

--
(E-Mail Removed)

 
Reply With Quote
 
Alex Monjushko
Guest
Posts: n/a
 
      06-17-2004
Eric Sosman <(E-Mail Removed)> 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 ((E-Mail Removed))
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      06-17-2004
In <(E-Mail Removed)> Eric Sosman <(E-Mail Removed)> writes:

>Dan Pop wrote:
>> In <(E-Mail Removed)> Eric Sosman <(E-Mail Removed)> 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: (E-Mail Removed)
 
Reply With Quote
 
Irrwahn Grausewitz
Guest
Posts: n/a
 
      06-17-2004
(E-Mail Removed) (Dan Pop) wrote:
>In <(E-Mail Removed)> Eric Sosman <(E-Mail Removed)> 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 ((E-Mail Removed))
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
sscanf feature in C++? Matt C++ 15 10-19-2004 04:36 PM
sscanf and c++! Mike C++ 3 06-09-2004 10:46 AM
endianness and sscanf/sprintf pramod C++ 22 01-06-2004 01:02 PM
Re: %n conversion in sscanf/VC++ 6-solved Anonymous C++ 0 10-30-2003 05:05 PM



Advertisments