473,908 Members | 5,327 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 7636
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
5602
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 :localhost "
2
2867
by: Mark Oliver | last post by:
Hi. What is the best replacement for sscanf? Thanks, Mark
3
4017
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
1859
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 also be: "0,0" or "0, 0"
3
9013
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 the call directly as I would in C++, instead of using custom classes).
20
21461
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
3511
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
6652
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 there any way to retrieve the string pointer position from sscanf so that I can just call it again...
9
2068
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
9875
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10913
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11042
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10536
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7246
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5930
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6134
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3355
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.