LinuxQuestions.org
Review your favorite Linux distribution.
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 09-06-2007, 09:08 AM   #1
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Rep: Reputation: 30
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(&regy, regnow, regflags);
 
        char *cleanstr = "this is a test oke?";
 
        if (regexec(&regy, 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.
 
Old 09-06-2007, 09:17 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 09-06-2007, 09:29 AM   #3
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Original Poster
Rep: Reputation: 30
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
 
Old 09-06-2007, 09:35 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 09-06-2007, 09:56 AM   #5
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Original Poster
Rep: Reputation: 30
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(&regy, 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(&regy, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
                regerror(error, &regy, 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
 
Old 09-06-2007, 11:17 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
How do you know whether the regex was properly compiled? You still haven't tested the return from regcomp().
--- rod.
 
Old 09-07-2007, 06:10 AM   #7
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Original Poster
Rep: Reputation: 30
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(&regy, regnow, regflags);
        printf("Compile regexp? %d\n",error);
        if (error) {
                regerror(error, &regy, errorBuf, maxLen);
                printf("Error1?: %s\n", errorBuf);
        }

        char *cleanstr = "this is a test oke?";
        memset(errorBuf, 0, maxLen);

        if ((error = regexec(&regy, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
                regerror(error, &regy, 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
 
Old 09-08-2007, 06:30 AM   #8
sitycent
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Rep: Reputation: 0
Quote:
Originally Posted by alitrix View Post
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(&regy, regnow, regflags);
        printf("Compile regexp? %d\n",error);
        if (error) {
                regerror(error, &regy, errorBuf, maxLen);
                printf("Error1?: %s\n", errorBuf);
        }

        char *cleanstr = "this is a test oke?";
        memset(errorBuf, 0, maxLen);

        if ((error = regexec(&regy, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
                regerror(error, &regy, 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
 
Old 09-08-2007, 10:56 AM   #9
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 09-12-2007, 02:31 AM   #10
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Original Poster
Rep: Reputation: 30
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(&regy, regnow, regflags);
   printf("Compile regexp? %d\n",error);
   if (error) {
      regerror(error, &regy, errorBuf, maxLen);
      printf("Error1?: %s\n", errorBuf);
   }   

   char *cleanstr = "this is a test oke?";
   memset(errorBuf, 0, maxLen);

   if ((error = regexec(&regy, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
      regerror(error, &regy, 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
 
Old 09-12-2007, 08:12 AM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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(&regy, regnow, regflags);
        printf("Compile regexp? %d\n",error);
        if (error) {
                regerror(error, &regy, errorBuf, maxLen);
                printf("Error1?: %s\n", errorBuf);
        }

        char *cleanstr = "this is a test oke?";
        memset(errorBuf, 0, maxLen);

        if ((error = regexec(&regy, cleanstr, MAX_MATCH, pmatch, 0))==REG_NOMATCH) {
                regerror(error, &regy, 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.
 
Old 09-12-2007, 08:28 AM   #12
alitrix
Member
 
Registered: Jun 2003
Location: Netherlands, The
Distribution: Ubuntu, Kernel 2.6.7
Posts: 169

Original Poster
Rep: Reputation: 30
How do u compile it?

I do: gcc regexTest.c -o regexTest
 
Old 09-12-2007, 10:15 AM   #13
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
I use the default make rules:
Code:
make regexTest
It echoes:
Code:
cc     regexTest.c   -o regexTest
cc is a symbolic link to gcc. Looks pretty similar to your command.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
regexp ovince Programming 8 06-17-2007 10:54 AM
Problem with regexp in mod_redirect on Apache 2 locodude Linux - Server 1 04-23-2007 09:20 PM
Regexp problem eremit Programming 6 06-23-2005 06:48 AM
PHP regexp problem abdul_zu Linux - Software 2 04-03-2005 07:35 AM
perl regexp problem raven Programming 4 03-21-2004 11:49 PM


All times are GMT -5. The time now is 03:13 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration