Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > regex.h question

Reply
Thread Tools

regex.h question

 
 
Knight
Guest
Posts: n/a
 
      02-16-2008
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]);
}
}
 
Reply With Quote
 
 
 
 
Knight
Guest
Posts: n/a
 
      02-16-2008
Follow up to self -
Because the beginning lambda of 111 matches [a-z]*. 111 would not
match '[a-z][a-z]*'.
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-16-2008
In article <(E-Mail Removed)>,
Knight <(E-Mail Removed)> 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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-16-2008
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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-18-2008
CBFalconer <(E-Mail Removed)> 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) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-18-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments