facing problem while using regexec

Discussion in 'C Programming' started by apoorva.groups, Dec 7, 2007.

  1. Hi

    I am facing problem while using regexec function.

    Ex: String = "abc_def_hig"
    sub string = "def"

    regexc if I use regexec the it will find the sub string in string and
    it will return 0.
    I want to modify the sub string such that it matches only if the
    string "starts" with the sub string.

    if String = "def_abc_hig and sub_String = "def"
    then only regexec should return 0
    else String is "abcdefhig" and sub_String = "def"
    the regexec should fail

    I tried using ^ in from of the sub string but then it does not event
    match the String.

    here is the code snippet.
    int regexpress ( char *string, char *pattern )
    regex_t * regex;
    printf("\nInside regex\n");
    regex = ( regex_t *) malloc (sizeof(regex_t));
    if(regcomp(regex, pattern, REG_EXTENDED)!=0) {
    return -1;
    if (regexec(regex, string, 0, NULL, 0)==0) {
    printf("\nInside regexpress pattern found\n");
    return 1;
    else {
    printf("\nInside else\n");
    return -22;


    retCode = regexpress( "abcdefhig", "def");
    if (retCode == 1){
    printf("\npattern found file found\n");
    return 1;

    retCode = regexpress( "defabchig", "def");
    if (retCode == 1){
    printf("\npattern found file found\n");
    return 1;


    I want that the Ist regexpress function to return 0 and 2nd regexpress
    function to return 1
    What Pattern should i be sending

    Thanks In Advance
    apoorva.groups, Dec 7, 2007
    1. Advertisements

  2. apoorva.groups

    Mark Bluemel Guest

    First point - there is no regexec function in Standard C. Many people
    would consider that you should ask your question in a group which
    discusses contexts where regexec exists.

    As regexec is part of POSIX, comp.unix.programmer seems a reasonable
    choice. (If my newsreader allowed me to set followups I would...)
    Second point. Even if you go to a more appropriate group, you will do
    better giving decent data. A small self-contained testcase is vastly
    better than a few snippets and a vague discussion.

    Put the effort in to reduce your program to the smallest version which
    illustrates the problem. Often by doing this, you'll solve the problem
    yourself. If you still can't fix it, cut and paste the sample into your
    posting, so that anyone else can see _exactly_ what the code is and

    As I happened to have a suitable system to hand, I rapidly produced a
    testcase which seemed to do what I expected from my manual pages. So
    you need to produce a much better explanation of your problem.

    When you've done so, please pursue this in a different newsgroup, not

    Here for reference is what I tried :-

    #include <stdio.h>
    #include <stdlib.h>
    #include <regex.h>
    void is_matched(char *string, char *pattern) {
    regex_t *reg = malloc(sizeof *reg);
    regcomp(reg,pattern,REG_EXTENDED); /* needs error handling!*/
    if (regexec(reg,string,(size_t)0,NULL,0) != 0) {
    printf("[%s] appears not to contain [%s]\n",string,pattern);
    } else {
    printf("[%s] appears to contain [%s]\n",string,pattern);

    int main(void) {
    char *strings[] = {"abcfedghi", "abcdefghi","defghijkl", NULL};
    char *patterns[] = {"def", "^def", NULL};
    char **nextString;
    char **nextPattern = patterns;
    for(nextPattern = patterns ; *nextPattern != NULL ; nextPattern++)
    for(nextString = strings ; *nextString != NULL ; nextString++)
    Mark Bluemel, Dec 7, 2007
    1. Advertisements

  3. This is (probably) the POSIX regexec function, so I would suggest you
    post in comp.unix.programmer. If you are using some other regexp
    library, then you should find another group to post to.

    The problem is not with the code you posted. When you post to
    comp.unix.programmer, post a whole, compilable, example program with a
    case that fails (adding ^ works for me with the code you posted).
    Ben Bacarisse, Dec 7, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.