LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-25-2010, 04:16 AM   #1
ppmoore
LQ Newbie
 
Registered: Sep 2006
Posts: 25

Rep: Reputation: 0
More question about regular expressions


Hello,

As a learning exercise, I'm trying to use regcomp() and regexec() to match the string
"H,2,1,1,"

My code is:

Code:
const char* test = "H,2,1,1,";
char* pattern = "H,[0-9],[0-9],[0-9]";
regex_t rexpr;
regmatch_t match[3];

if( ( result = regcomp( &rexpr, pattern, 0 ) ) )
{
  printf("Cannot compile pattern");
  exit();
}

if( regexec(&rexpr, test, 1, match, 0 ) ) 
{
  printf("Cannot parse string");
  exit();
}
The pattern matches, as expected.

When I change the pattern to any of the following, it fails
Code:
"H,[0-9]+,[0-9]+,[0-9]+"   // match 1 or more of each digit
"H,[0-9]*,[0-9]*,[0-9]*"   // match 0 or more of each digit
"H(,[0-9])+"               // match 1 or more groups of ,<digit>
"H(,[0-9]){3}"             // match 3 groups of ,<digit>
I tested each of the above on several regex test sites, and they work. Is the regex implementation included in the standard C library more limited than for other implementations?

Many thanks,
Paul
 
Old 11-25-2010, 06:10 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
When I change the pattern to any of the following, it fails
Does this mean you receive an error?
It does not compile?
Simply does not display that it matched?

As you can see, failure could mean any number of things so a little more detail might help?

Also, based on the snippet you have provided there does not appear to be any way to tell if success or not, assuming it passes the 2 included tests.
 
Old 11-25-2010, 06:42 AM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ppmoore View Post
Hello,

As a learning exercise, I'm trying to use regcomp() and regexec() to match the string
"H,2,1,1,"

My code is:

Code:
const char* test = "H,2,1,1,";
char* pattern = "H,[0-9],[0-9],[0-9]";
regex_t rexpr;
regmatch_t match[3];

if( ( result = regcomp( &rexpr, pattern, 0 ) ) )
{
  printf("Cannot compile pattern");
  exit();
}

if( regexec(&rexpr, test, 1, match, 0 ) ) 
{
  printf("Cannot parse string");
  exit();
}
The pattern matches, as expected.

When I change the pattern to any of the following, it fails
Code:
"H,[0-9]+,[0-9]+,[0-9]+"   // match 1 or more of each digit
"H,[0-9]*,[0-9]*,[0-9]*"   // match 0 or more of each digit
"H(,[0-9])+"               // match 1 or more groups of ,<digit>
"H(,[0-9]){3}"             // match 3 groups of ,<digit>
I tested each of the above on several regex test sites, and they work. Is the regex implementation included in the standard C library more limited than for other implementations?

Many thanks,
Paul
Why do you have '1' here:

Code:
if( regexec(&rexpr, test, 1, match, 0 ) )
while you have '3' here:

Code:
regmatch_t match[3];
...

And how about '0' here:

Code:
if( regexec(&rexpr, test, 1, match, 0 ) )
- maybe you need REG_EXTENDED ?

?
 
Old 11-25-2010, 08:15 AM   #4
ppmoore
LQ Newbie
 
Registered: Sep 2006
Posts: 25

Original Poster
Rep: Reputation: 0
Thanks Sergei,

I had also independently found that not using REG_EXTENDED was the problem, and was going to update the thread when I found your reply.

Best,
Paul
 
Old 11-25-2010, 09:37 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Glad you got there in the end Please mark as SOLVED. You may even want to copy your solution in for others to understand where you went wrong
 
  


Reply



Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular Expressions Wim Sturkenboom Programming 10 11-19-2009 01:21 AM
Perl - Regular Expressions question... gliesian Programming 2 04-10-2008 07:19 PM
regular expressions. stomach Linux - Software 1 02-10-2006 06:41 AM
Regular Expressions markjuggles Programming 2 05-05-2005 11:39 AM
help with REGULAR EXPRESSIONS ner Linux - General 23 10-31-2003 11:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration