regular expressions in C

  • Thread starter Ramprasad A Padmanabhan
  • Start date
R

Ramprasad A Padmanabhan

Hi all,

I am a perl programmer and a newbie in C. Can Someone tell me How
Can I use regular expressions in C ( on Unix ) .

Suppose I am parsing a string like
"Ram Prasad" <[email protected]>
or
Ram<[email protected]>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = (e-mail address removed)

Is there a library I can use to do this

Thanks
Ram
 
F

Friedrich Dominicus

Ramprasad A Padmanabhan said:
Hi all,

I am a perl programmer and a newbie in C. Can Someone tell me How
Can I use regular expressions in C ( on Unix ) .


Suppose I am parsing a string like
"Ram Prasad" <[email protected]>
or
Ram<[email protected]>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = (e-mail address removed)
regular expressions are not part of Standard C, you might find some
libraries for it. I suggest searching for PCRE might give you a "sort"
of déja-vu.

Regards
Friedrich
 
E

Eric

Ramprasad A Padmanabhan said:
Hi all,

I am a perl programmer and a newbie in C. Can Someone tell me How
Can I use regular expressions in C ( on Unix ) .

You will need to either roll your own parser or find a library (perhaps
provided by your system) that has implemented it.
 
D

Dan Pop

In said:
I am a perl programmer and a newbie in C. Can Someone tell me How
Can I use regular expressions in C ( on Unix ) .

Find a regular expression library for C and do whatever its documentation
says. C doesn't provide any intrinsic support for regular expressions.
Suppose I am parsing a string like
"Ram Prasad" <[email protected]>
or
Ram<[email protected]>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = (e-mail address removed)

Is there a library I can use to do this

For such a trivial case, the parsing abilities of sscanf are enough:

char name[50 + 1], email[50 + 1];
char *string = "\"Ram Prasad\" <[email protected]>";
int rc = sscanf(string, "%50[^<]<%50[^>]", name, email);

will give you a trailing space after "Ram Prasad", but you can trivially
get rid of it, if needed. The same sscanf call can properly parse both
formats you mentioned. If rc != 2, the input string couldn't be parsed
as desired.

Have a look at the scanf specification to understand how it works (it's
fairly straightforward to someone used to Perl's regular expressions).

Dan
 
C

CBFalconer

Ramprasad said:
Suppose I am parsing a string like
"Ram Prasad" <[email protected]>
or
Ram<[email protected]>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = (e-mail address removed)

Is there a library I can use to do this

You might investigate lex. (which is OT here). However this is
such a simple scanner that you should easily be able to create
custom code for the purpose, after you decide what separates the
various parts when and what is just noise.
 
V

Vijay Kumar R Zanvar

[..]
char name[50 + 1], email[50 + 1];
char *string = "\"Ram Prasad\" <[email protected]>";
int rc = sscanf(string, "%50[^<]<%50[^>]", name, email);


You missed out conversion specifier:

int rc = sscanf(string, "%50[^<]s%50[^>]s", name, email);

[..]
 
D

Dan Pop

[..]
char name[50 + 1], email[50 + 1];
char *string = "\"Ram Prasad\" <[email protected]>";
int rc = sscanf(string, "%50[^<]<%50[^>]", name, email);


You missed out conversion specifier:

int rc = sscanf(string, "%50[^<]s%50[^>]s", name, email);

Nope, I didn't. Free clue: %s and %[ are TWO *different* conversion
specifiers.

Dan
 
S

stelios xanthakis

Ramprasad A Padmanabhan said:
Hi all,

I am a perl programmer and a newbie in C. Can Someone tell me How
Can I use regular expressions in C ( on Unix ) .

Suppose I am parsing a string like
"Ram Prasad" <[email protected]>
or
Ram<[email protected]>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = (e-mail address removed)

Is there a library I can use to do this

The most efficient way to do it is with strchr ().
As for a library suggestion, I may add lwc which
has a regexp-to-C code generator. For the regexp:

RegExp email ("(.+\w)\s*<([^>]*)");

It will generate a function called email_match(char*, struct charp_len[]);
which can match strings and extract matches.

Only tested to work with gcc/glibc system though.

The URL: <http://students.ceid.upatras.gr/~sxanth/lwc/index.html>
And an example of what kind of code is generated:
<http://students.ceid.upatras.gr/~sxanth/lwc/ex15.html>


stelios
 

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

Members online

No members online now.

Forum statistics

Threads
474,303
Messages
2,571,557
Members
48,359
Latest member
Raqib_121635
Top