Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > regular expression, help needed

regular expression, help needed
Thread Tools
Forum Regular
Join Date: Jan 2001
Status: Offline
Reply With Quote
Nov 4, 2004, 02:44 AM
 
If I want to match strings on the following form:

%s %i ( %f %f %f ) (%f %f % f)

where
%s is letters, e.g a string
%i a integer, posetiv or negative
%f a float, can be 0 or 0.0,

ex:

"green" 3 (0 0 0) (-0.4 0.00002 0.222) (4.56 -10.3232 2.533)

or

"red" -6 (0.000012 -0.23131 0.2321) (-0.4 0.00002 0.222) (4.56 -10.3232 2.533)

How can a match these with a regular expression, Im using the regcomp/regexec functions in c.

in Java I would use a expression like this:

\"([a-z]+)\"\\s(-*[0-9]+)\\s\\(\\s(-*[0-9]*\\.*[0-9]*)\\s(-*[0-9]*\\.*[0-9]*)\\s(-*[0-9]*\\.*[0-9]*)\\s\\)\\s\\(\\s(-*[0-9]*\\.*[0-9]*)\\s(-*[0-9]*\\.*[0-9]*)\\s(-*[0-9]*\\.*[0-9]*)\\s\\)

this piece of code should do the trick in Java

Code:
public static void main(String[] args) { StringBuffer buf = new StringBuffer("\"origin\" -1 ( 0 0 0 ) ( -0.4999999404 -0.5000000596 -0.5000000596 )"); StringBuffer parsed; String exp = "\"([a-z]+)\"\\s(-*[0-9]+)\\s\\(\\s(-*[0-9]*\\.*[0-9]*)\\s(-*[0-9]*\\.*[0-9]*)\\s(-*[0-9]*\\.*[0-9]*)\\s\\)\\s\\(\\s(-*[0-9]*\\.*[0-9]*)\\s(-*[0-9]*\\.*[0-9]*)\\s(-*[0-9]*\\.*[0-9]*)\\s\\)"; Pattern p = Pattern.compile(exp); Matcher m = p.matcher(buf); while(m.find()){ System.out.println(m.group(0)); System.out.println(m.group(1)); System.out.println(m.group(2)); System.out.println(m.group(3)+","+m.group(4)+","+m.group(5)); System.out.println(m.group(6)+","+m.group(7)+","+m.group(8)); } }
can somebody please help me get this to work in c?
     
Professional Poster
Join Date: Sep 1999
Location: Ottawa, ON, Canada
Status: Offline
Reply With Quote
Nov 4, 2004, 09:02 AM
 
Search the web for pattern matching code in C. I don't think there is any in the standard C libraries.
     
geran  (op)
Forum Regular
Join Date: Jan 2001
Status: Offline
Reply With Quote
Nov 4, 2004, 11:03 AM
 
in the GNU C Library there is pattern matching functions:

regcomp and regexec, look at the man pages, ex man regcomp. I dont get the syntax of the pattern to work as expected.

It's some differences between Java's regular expression and the c implementation, but I can't find any usefull information on how to create the pattern. So what I need is someone that is experinced with the regexec function to help me with the syntax of the pattern.
     
geran  (op)
Forum Regular
Join Date: Jan 2001
Status: Offline
Reply With Quote
Nov 5, 2004, 09:41 AM
 
I found the answer, you must set the flag to regcomp to REG_EXTENDED.

then an expresion like:

"\"([a-zA-Z0-9\\_]+)\"[[:space:]]*(-?[0-9]+)[[:space:]]*\\([[:space:]]*(-?[0-9]+[.]*[0-9]*)[[:space:]]*(-?[0-9]+[.]*[0-9]*)[[:space:]]*(-?[0-9]+[.]*[0-9]*)[[:space:]]*\\)[[:space:]]*\\([[:space:]]*(-?[0-9]+[.]*[0-9]*)[[:space:]]*(-?[0-9]+[.]*[0-9]*)[[:space:]]*(-?[0-9]+[.]*[0-9]*)[[:space:]]*\\)

can be used, I just love the documentation ...
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:19 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2