473,395 Members | 1,516 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,395 software developers and data experts.

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

Similar topics

4
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...
2
by: Mark Oliver | last post by:
Hi. What is the best replacement for sscanf? Thanks, Mark
3
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...
4
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...
3
by: ewolfman | last post by:
Hi all, I've had a look at this post:...
20
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
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...
9
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...
9
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...

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.