Thread: Regexec problem

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    3

    [SOLVED] Regexec problem

    Hi all, I'm new here.

    Just having problems using regexec in C

    I want to match a simple pattern several times, like "(string)", but I always get only one result.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <regex.h>
    
    int main() {
            regex_t r;
            char *str = "trying string one string two";
            regmatch_t matches[4];
            int c;
    
            regcomp(&r, "(string)", REG_EXTENDED | REG_ICASE);
            regexec(&r, str, 4, matches, 0);
    
            for(c=0; c<4; c++)
                    printf("%d\n", matches[c].rm_so);
            regfree(&r);
    
            return 0;
    }
    the output is this:

    Code:
    7
    7
    -1
    -1
    Tryed the same expression in ruby, and it returned both elements, so I suppose the pattern expression is OK.

    What am I doing wrong?
    thanks
    Last edited by tigrezno; 12-08-2010 at 07:36 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You've only got one set of parentheses, so how that can match multiple items I wouldn't know. Regexes only run once per line.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    Quote Originally Posted by tabstop View Post
    You've only got one set of parentheses, so how that can match multiple items I wouldn't know. Regexes only run once per line.
    can you elaborate on that?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We don't have a mechanism like the /g modifier in Perl (at least that I know of, but I don't use C regexes much to speak of) to re-run the pattern on a string. You run the regex on the string, it looks for the longest thing it can find that matches "(string)", finds it at character 7, and that's it. You've matched the entire pattern and you're done. It doesn't take the remainder of the string and run the regex again on the remainder.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    3
    oh I understand, So, i'll have to move with some pointer over the string and repeating the match.

    thanks you so much, problem solved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM

Tags for this Thread