regex.h question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Knight

    regex.h question

    Hi,
    in the following program, compiled using gcc on Linux, and invoked
    as
    a.out '[a-z]*' '111'
    I get '111' matches '[a-z]*'. What am I doing wrong?

    #include <sys/types.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <regex.h>

    int main (int argc, char *argv[])
    {
    regex_t preg;
    char errtext[100];
    int rc;

    if (rc = regcomp(&preg, argv[1], REG_EXTENDED|RE G_NOSUB)) {
    regerror(rc, &preg, errtext, 100);
    printf("%s\n", errtext);
    } else if (rc = regexec(&preg, argv[2], 0, NULL, 0)) {
    regerror(rc, &preg, errtext, 100);
    printf("%s\n", errtext);
    } else {
    printf("'%s' matches '%s'\n", argv[2], argv[1]);
    }
    }
  • Knight

    #2
    Re: regex.h question

    Follow up to self -
    Because the beginning lambda of 111 matches [a-z]*. 111 would not
    match '[a-z][a-z]*'.

    Comment

    • Walter Roberson

      #3
      Re: regex.h question

      In article <4811e6bc-8429-4c77-bcae-a2dae0073bae@s1 9g2000prg.googl egroups.com>,
      Knight <knighttof3@yah oo.comwrote:
      >Hi,
      in the following program, compiled using gcc on Linux, and invoked
      >as
      >a.out '[a-z]*' '111'
      I get '111' matches '[a-z]*'. What am I doing wrong?
      >
      >#include <sys/types.h>
      sys/types.h is not part of Standard C.
      >#include <stdio.h>
      >#include <stdlib.h>
      >#include <regex.h>
      regex.h is not part of standard C.
      >
      >int main (int argc, char *argv[])
      >{
      regex_t preg;
      char errtext[100];
      int rc;
      >
      if (rc = regcomp(&preg, argv[1], REG_EXTENDED|RE G_NOSUB)) {
      regerror(rc, &preg, errtext, 100);
      regcomp() and regerror() are not part of standard C.
      printf("%s\n", errtext);
      } else if (rc = regexec(&preg, argv[2], 0, NULL, 0)) {
      regerror(rc, &preg, errtext, 100);
      printf("%s\n", errtext);
      } else {
      printf("'%s' matches '%s'\n", argv[2], argv[1]);
      }
      >}
      I get '111' matches '[a-z]*'. What am I doing wrong?
      [OT]

      It appears to me that what you are doing wrong is thinking that
      '111' does *not* match '[a-z]*' . Is it not true that there
      is at least one place in '111' that there is an occurance
      of zero or more characters in the range a-z ? '*' in a
      regular expression means zero or more. Are there not 0
      alphabetic letters between the first '1' and the second '1' ?

      You may wish to try [a-z]+ or you may wish to anchor your search.
      --
      "There's no term to the work of a scientist." -- Walter Reisch

      Comment

      • CBFalconer

        #4
        Re: regex.h question

        Knight wrote:
        >
        in the following program, compiled using gcc on Linux, and
        invoked as
        a.out '[a-z]*' '111'
        I get '111' matches '[a-z]*'. What am I doing wrong?
        >
        #include <stdio.h>
        #include <stdlib.h>
        #include <sys/types.h>
        #include <regex.h>
        You are including the above include files, which don't exist in
        standard C.

        --
        [mail]: Chuck F (cbfalconer at maineline dot net)
        [page]: <http://cbfalconer.home .att.net>
        Try the download section.



        --
        Posted via a free Usenet account from http://www.teranews.com

        Comment

        • Keith Thompson

          #5
          Re: regex.h question

          CBFalconer <cbfalconer@yah oo.comwrites:
          Knight wrote:
          >in the following program, compiled using gcc on Linux, and
          >invoked as
          > a.out '[a-z]*' '111'
          >I get '111' matches '[a-z]*'. What am I doing wrong?
          >>
          >#include <stdio.h>
          >#include <stdlib.h>
          >
          >#include <sys/types.h>
          >#include <regex.h>
          >
          You are including the above include files, which don't exist in
          standard C.
          No, that's not what he's doing wrong. There is nothing wrong with
          using headers that are not defined by the C standard, as long as
          you're aware that your code won't be portable to all C
          implementations . Not all C programs can be, or should be, 100%
          portable.

          What he's doing wrong is posting this question to comp.lang.c (which
          deals with standard C) rather than to comp.unix.progr ammer (which
          deals with the system that defines the <sys/types.hand <regex.h>
          headers). That's a minor offense (I hesitate even to use the word
          "offense"), which is easily corrected by telling the OP where to post.
          I believe that was already done several days ago.

          Chuck, I understand that you download news articles in batches.
          That's fine, but if you could post your responses as soon as possible
          after downloading the articles, you could avoid a lot of situations
          like this one where you post responses to questions that have already
          been answered.

          --
          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • CBFalconer

            #6
            Re: regex.h question

            Keith Thompson wrote:
            >
            .... snip ...
            >
            Chuck, I understand that you download news articles in batches.
            That's fine, but if you could post your responses as soon as
            possible after downloading the articles, you could avoid a lot
            of situations like this one where you post responses to
            questions that have already been answered.
            If you look at the posting date/time you will find it is accurate
            within about 1 or 2 hours. However I have the joy of a news-server
            that chooses (at times) to absorb these for something like 5 days
            and then actually post them. Drives me nuts. Sooner or later I am
            going to dump teranews.

            --
            [mail]: Chuck F (cbfalconer at maineline dot net)
            [page]: <http://cbfalconer.home .att.net>
            Try the download section.



            --
            Posted via a free Usenet account from http://www.teranews.com

            Comment

            Working...