facing problem while using regexec

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

  1. Guest

    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) {
    printf("\nERROR\n");
    return -1;
    }
    if (regexec(regex, string, 0, NULL, 0)==0) {
    printf("\nInside regexpress pattern found\n");
    regfree(regex);
    free(regex);
    return 1;
    }
    else {
    printf("\nInside else\n");
    regfree(regex);
    free(regex);
    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
    , Dec 7, 2007
    #1
    1. Advertising

  2. Mark Bluemel Guest

    wrote:
    > Hi
    >
    > I am facing problem while using regexec function.


    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...)

    > 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.


    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
    does.

    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
    comp.lang.c.

    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);
    }
    regfree(reg);
    }

    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++)
    is_matched(*nextString,*nextPattern);
    }
    Mark Bluemel, Dec 7, 2007
    #2
    1. Advertising

  3. "" <> writes:

    > I am facing problem while using regexec function.


    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.

    <snip>
    > I want to modify the sub string such that it matches only if the
    > string "starts" with the sub string.

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


    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.
    Ben Bacarisse, Dec 7, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anubhav Jain
    Replies:
    0
    Views:
    444
    Anubhav Jain
    Mar 20, 2006
  2. Replies:
    4
    Views:
    19,311
  3. Gregory Toomey

    Posix regexec - matching subexpression

    Gregory Toomey, Feb 13, 2005, in forum: C Programming
    Replies:
    6
    Views:
    623
    Richard Bos
    Feb 23, 2005
  4. zoltan
    Replies:
    2
    Views:
    403
    zoltan
    Mar 13, 2006
  5. Replies:
    3
    Views:
    941
    Mark Bluemel
    Feb 4, 2008
Loading...

Share This Page