regex.h question

K

Knight

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]);
}
}
 
K

Knight

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

Walter Roberson

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

CBFalconer

Knight said:
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.
 
K

Keith Thompson

CBFalconer said:
Knight said:
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.
 
C

CBFalconer

Keith said:
.... 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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top