473,561 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7588
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******@engine er.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*******@free net.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******@engine er.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*********@su n.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*********@su n.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*********@su n.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*******@hotm ail.com)
Nov 14 '05 #8
Da*****@cern.ch (Dan Pop) wrote:
In <40************ **@sun.com> Eric Sosman <Er*********@su n.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*******@free net.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*********@su n.com> writes:
Dan Pop wrote:
In <40************ **@sun.com> Eric Sosman <Er*********@su n.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(bu f, "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
interpretati on 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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5588
by: Da Wang | last post by:
Hi, all I am trying to use sscanf to parse the header for a web server, according to the requirement, it need to neglect all the blanks in the header for example, all the following should be equvalient and the value should be read correctly( get "Host" and "localhost" ) " Host: localhost " " Host : localhost " " Host ...
2
2853
by: Mark Oliver | last post by:
Hi. What is the best replacement for sscanf? Thanks, Mark
3
4002
by: Evan Mathias | last post by:
Does anyone know a equivalent to sscanf from c++. I would appreciate a good answer to this one, since currently I have had to write a library of regular expressions to recognise integers, floats etc.. and I dont want to use exceptions or anything else that is slow or impractical. Much thanks.
4
1846
by: sklett | last post by:
given one of these two string: ProtocolRecord(0, 100, 100, arZeroMax , 1, 0, SKIP,0), ProtocolRecord(0, 100, 100, arSetSet , 4, 0, DJMPNZSTOP, (void *) &Protocol_TD) I want to pull all the values out that are separated by commas and the last value. I have no guarantee that there won't be a space before the comma (IE: "0 , 0") they could...
3
8974
by: ewolfman | last post by:
Hi all, I've had a look at this post: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/a5bec880d8efbb92/184ca9e25136494b?lnk=st&q=&rnum=1&hl=en#184ca9e25136494b But I couldn't get it to work. In short, how can I call scanf or sscanf directly from C# (i.e. I'd rather have a DllImport and perform...
20
21412
by: AMP | last post by:
Hello, Anybody know if anything exists like sscanf in c. I found a few things OL but most were pretty old. Maybe something has come along since 2004? Thanks Mike
5
3491
by: Alex Mathieu | last post by:
Hi, using sscanf, I'm trying to retrieve something, but nothing seems to work. Here's the pattern: SS%*sț0ț%6s Heres the data: SS000000395000000000DC-ț0ț799829ț1174503725ț Actually, I would like to retrieve the "799829" from the data, but it always failed. I thought that the "%*sț0ț" would work as if I was
9
6637
by: Bint | last post by:
i have a string "success=1&u=0&name=bint&u=1&name=lucy&u=2&name=barry" etc i can use sscanf(string,"success=%d", &d) to get the success value. but after that i just want to read name and u pairs until there are no more. if Iwere to do a sscanf (string,"success=%d&u=%d&name=%s"), that would get me the values of the first u/name, right? is...
9
2051
by: Tim Streater | last post by:
I'm obviously missing something here. I have a line like this (in $line), and I want all the text within the double quotes:: somestr="string I want" with some whitespace at the beginning. So I try this: $res = sscanf ($line, ' %s"%s"'); $wanted = $res;
0
7644
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7568
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7614
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6206
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3598
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1180
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.