regex.h question

Discussion in 'C Programming' started by Knight, Feb 16, 2008.

  1. Knight

    Knight Guest

    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|REG_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, Feb 16, 2008
    #1
    1. Advertising

  2. Knight

    Knight Guest

    Follow up to self -
    Because the beginning lambda of 111 matches [a-z]*. 111 would not
    match '[a-z][a-z]*'.
     
    Knight, Feb 16, 2008
    #2
    1. Advertising

  3. In article <>,
    Knight <> wrote:
    >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|REG_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
     
    Walter Roberson, Feb 16, 2008
    #3
  4. Knight

    CBFalconer Guest

    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
     
    CBFalconer, Feb 16, 2008
    #4
  5. CBFalconer <> writes:
    > 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.programmer (which
    deals with the system that defines the <sys/types.h> and <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_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 18, 2008
    #5
  6. Knight

    CBFalconer Guest

    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
     
    CBFalconer, Feb 18, 2008
    #6
    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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    746
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,693
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    630
  4. Xah Lee
    Replies:
    1
    Views:
    973
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    834
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page