By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,409 Members | 952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,409 IT Pros & Developers. It's quick & easy.

regular expressions in C

P: n/a
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" <ra*@netcore.co.in>
or
Ram<ra*@netcore.co.in>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = ra*@netcore.co.in

Is there a library I can use to do this

Thanks
Ram

Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Ramprasad A Padmanabhan <ra*******@netcore.co.in> writes:
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" <ra*@netcore.co.in>
or
Ram<ra*@netcore.co.in>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = ra*@netcore.co.in

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
Nov 13 '05 #2

P: n/a
Ramprasad A Padmanabhan <ra*******@netcore.co.in> wrote:
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.
--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
Nov 13 '05 #3

P: n/a
In <bq*************@ID-166953.news.uni-berlin.de> Ramprasad A Padmanabhan <ra*******@netcore.co.in> writes:
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" <ra*@netcore.co.in>
or
Ram<ra*@netcore.co.in>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = ra*@netcore.co.in

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\" <ra*@netcore.co.in>";
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4

P: n/a
Ramprasad A Padmanabhan wrote:

Suppose I am parsing a string like
"Ram Prasad" <ra*@netcore.co.in>
or
Ram<ra*@netcore.co.in>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = ra*@netcore.co.in

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #5

P: n/a

[..]

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

You missed out conversion specifier:

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

[..]
Nov 13 '05 #6

P: n/a
In <bq*************@ID-203837.news.uni-berlin.de> "Vijay Kumar R Zanvar" <vi*****@hotpop.com> writes:

[..]

char name[50 + 1], email[50 + 1];
char *string = "\"Ram Prasad\" <ra*@netcore.co.in>";
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #7

P: n/a
Ramprasad A Padmanabhan <ra*******@netcore.co.in> wrote in message news:<bq*************@ID-166953.news.uni-berlin.de>...
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" <ra*@netcore.co.in>
or
Ram<ra*@netcore.co.in>

And I want the email id seperated from the name
such that (for the first case )
name = "Ram Prasad"
email = ra*@netcore.co.in

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
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.