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
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
09-06-2007, 09:08 AM
#1
Member
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169
Rep:
Problem with regexp in c
I have the next code:
Code:
#include <stdio.h>
#include <regex.h>
int main () {
int MAX_MATCH=5;
regmatch_t pmatch[MAX_MATCH];
int regflags=REG_ICASE|REG_EXTENDED;
regex_t regy;
char *ptr;
char *regnow = "\\wtest\\w";
regcomp(®y, regnow, regflags);
char *cleanstr = "this is a test oke?";
if (regexec(®y, cleanstr, MAX_MATCH, pmatch, 0)==REG_NOMATCH) {
printf("No match found for: %s\n", cleanstr);
} else {
printf("Match found! %s\n",cleanstr);
printf("start: %d - end: %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);
}
return 0;
}
But I'm getting No match found.
It's like every \ is ignored.
If I try \\btest\\b, it doesn't work as well.
What am I doing wrong
?
Greetz,
alitrix
Last edited by alitrix; 09-06-2007 at 09:30 AM .
09-06-2007, 09:17 AM
#2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
You should test the return value from
regcomp() . It may be complaining about
:
Code:
char *regnow = "\\wtest\w";
^
... which seems to be missing one backslash.
--- rod.
09-06-2007, 09:29 AM
#3
Member
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169
Original Poster
Rep:
It isn't that
That was just a typo when I copyed the code to here and modified it.
Even with \\wtest\\w it's not working :s
09-06-2007, 09:35 AM
#4
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Still, you should test the return value from all applicable functions. They return a status value to help you deal with errors. If you ignore what it is telling you, you will not be able to handle errors. There is even the regerror() function that makes the error messages more human readable.
--- rod.
09-06-2007, 09:56 AM
#5
Member
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169
Original Poster
Rep:
Code:
#include <stdio.h>
#include <regex.h>
int main () {
int MAX_MATCH=5;
regmatch_t pmatch[MAX_MATCH];
int regflags=REG_ICASE|REG_EXTENDED;
regex_t regy;
char *ptr;
char *regnow = "\\wtest\\w";
regcomp(®y, regnow, regflags);
char *cleanstr = "this is a test oke?";
int error;
int maxLen=512;
char *errorBuf = malloc(maxLen);
memset(errorBuf, 0, maxLen);
if ((error = regexec(®y, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
regerror(error, ®y, errorBuf, maxLen);
printf("No match found for %d: %s\n", error, cleanstr);
printf("Error?: %s\n", errorBuf);
} else {
printf("Match found! %s\n",cleanstr);
printf("start: %d - end: %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);
}
return 0;
}
Output:
Code:
No match found for 1: this is a test oke?
Error?: regexec() failed to match
09-06-2007, 11:17 AM
#6
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
How do you know whether the regex was properly compiled? You still haven't tested the return from regcomp() .
--- rod.
09-07-2007, 06:10 AM
#7
Member
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169
Original Poster
Rep:
Well, the regex is compiled correctly.
Code:
#include <stdio.h>
#include <regex.h>
int main () {
int MAX_MATCH=5;
regmatch_t pmatch[MAX_MATCH];
int regflags=REG_ICASE|REG_EXTENDED;
regex_t regy;
char *ptr;
char *regnow = "\\wtest\\w";
int maxLen=512;
char *errorBuf = malloc(maxLen);
int error;
error = regcomp(®y, regnow, regflags);
printf("Compile regexp? %d\n",error);
if (error) {
regerror(error, ®y, errorBuf, maxLen);
printf("Error1?: %s\n", errorBuf);
}
char *cleanstr = "this is a test oke?";
memset(errorBuf, 0, maxLen);
if ((error = regexec(®y, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
regerror(error, ®y, errorBuf, maxLen);
printf("No match found for %d: %s\n", error, cleanstr);
printf("Error2?: %s\n", errorBuf);
} else {
printf("Match found! %s\n",cleanstr);
printf("start: %d - end: %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);
}
return 0;
}
Output
Code:
Compile regexp? 0
No match found for 1: this is a test oke?
Error2?: regexec() failed to match
09-08-2007, 06:30 AM
#8
LQ Newbie
Registered: Sep 2007
Posts: 2
Rep:
Quote:
Originally Posted by
alitrix
Well, the regex is compiled correctly.
Code:
#include <stdio.h>
#include <regex.h>
int main () {
int MAX_MATCH=5;
regmatch_t pmatch[MAX_MATCH];
int regflags=REG_ICASE|REG_EXTENDED;
regex_t regy;
char *ptr;
char *regnow = "\\wtest\\w";
int maxLen=512;
char *errorBuf = malloc(maxLen);
int error;
error = regcomp(®y, regnow, regflags);
printf("Compile regexp? %d\n",error);
if (error) {
regerror(error, ®y, errorBuf, maxLen);
printf("Error1?: %s\n", errorBuf);
}
char *cleanstr = "this is a test oke?";
memset(errorBuf, 0, maxLen);
if ((error = regexec(®y, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
regerror(error, ®y, errorBuf, maxLen);
printf("No match found for %d: %s\n", error, cleanstr);
printf("Error2?: %s\n", errorBuf);
} else {
printf("Match found! %s\n",cleanstr);
printf("start: %d - end: %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);
}
return 0;
}
Output
Code:
Compile regexp? 0
No match found for 1: this is a test oke?
Error2?: regexec() failed to match
char *cleanstr = "this is a test oke?";
memset(errorBuf, 0, maxLen
09-08-2007, 10:56 AM
#9
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Okay, after building and running your code, a few observations.
For
malloc() you should include
malloc.h
For
memset() , you should include
string.h
The biggy, and I can't believe I didn't pick up on this, is the regex. I think the metachars should be
\s , not
\w , since you are trying to match whitespace, not word chars. This worked for me.
Code:
char *regnow = "\\stest\\s";
--- rod.
09-12-2007, 02:31 AM
#10
Member
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169
Original Poster
Rep:
Strange, can u show me your code?
It isn't working here
This is what I have:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
int main () {
int MAX_MATCH=5;
regmatch_t pmatch[MAX_MATCH];
int regflags=REG_ICASE|REG_EXTENDED;
regex_t regy;
char *ptr;
char *regnow = "\\stest\\s";
int maxLen=512;
char *errorBuf = malloc(maxLen);
int error;
error = regcomp(®y, regnow, regflags);
printf("Compile regexp? %d\n",error);
if (error) {
regerror(error, ®y, errorBuf, maxLen);
printf("Error1?: %s\n", errorBuf);
}
char *cleanstr = "this is a test oke?";
memset(errorBuf, 0, maxLen);
if ((error = regexec(®y, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
regerror(error, ®y, errorBuf, maxLen);
printf("No match found for %d: %s\n", error, cleanstr);
printf("Error2?: %s\n", errorBuf);
} else {
printf("Match found! %s\n",cleanstr);
printf("start: %d - end: %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);
}
return 0;
}
Output
Code:
./regex
Compile regexp? 0
No match found for 1: this is a test oke?
Error2?: regexec() failed to match
09-12-2007, 08:12 AM
#11
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Sure. It's really unchanged, except those changes that I mentioned.
Code:
[bomr@localhost c]$ more regexTest.c
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <regex.h>
int main () {
int MAX_MATCH=5;
regmatch_t pmatch[MAX_MATCH];
int regflags=REG_ICASE|REG_EXTENDED;
regex_t regy;
char *ptr;
char *regnow = "\\stest\\s";
int maxLen=512;
char *errorBuf = malloc(maxLen);
int error;
error = regcomp(®y, regnow, regflags);
printf("Compile regexp? %d\n",error);
if (error) {
regerror(error, ®y, errorBuf, maxLen);
printf("Error1?: %s\n", errorBuf);
}
char *cleanstr = "this is a test oke?";
memset(errorBuf, 0, maxLen);
if ((error = regexec(®y, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
regerror(error, ®y, errorBuf, maxLen);
printf("No match found for %d: %s\n", error, cleanstr);
printf("Error2?: %s\n", errorBuf);
} else {
printf("Match found! %s\n",cleanstr);
printf("start: %d - end: %d\n", pmatch[0].rm_so, pmatch[0].rm_eo);
}
return 0;
}
The runtime results:
Code:
[bomr@localhost c]$ ./regexTest
Compile regexp? 0
Match found! this is a test oke?
start: 9 - end: 15
Built on Fedora Core 5: gcc (GCC) 4.1.1 20070105 (Red Hat 4.1.1-51)
--- rod.
09-12-2007, 08:28 AM
#12
Member
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169
Original Poster
Rep:
How do u compile it?
I do: gcc regexTest.c -o regexTest
09-12-2007, 10:15 AM
#13
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
I use the default
make rules:
It echoes:
Code:
cc regexTest.c -o regexTest
cc is a symbolic link to
gcc . Looks pretty similar to your command.
--- rod.
All times are GMT -5. The time now is 06:39 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News