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

regular expressions in C

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
7 40965
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
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
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
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

[..]

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

Similar topics

8
by: Michael McGarry | last post by:
Hi, I am horrible with Regular Expressions, can anyone recommend a book on it? Also I am trying to parse the following string to extract the number after load average. ".... load average:...
1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
2
by: Sehboo | last post by:
Hi, I have several regular expressions that I need to run against documents. Is it possible to combine several expressions in one expression in Regex object. So that it is faster, or will I...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
3
by: a | last post by:
I'm a newbie needing to use some Regular Expressions in PHP. Can I safely use the results of my tests using 'The Regex Coach' (http://www.weitz.de/regex-coach/index.html) Are the Regular...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
13
by: Wiseman | last post by:
I'm kind of disappointed with the re regular expressions module. In particular, the lack of support for recursion ( (?R) or (?n) ) is a major drawback to me. There are so many great things that can...
12
by: FAQEditor | last post by:
Anybody have any URL's to tutorials and/or references for Regular Expressions? The four I have so far are: http://docs.sun.com/source/816-6408-10/regexp.htm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...

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.