Unix Programming - String OR not working

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > March 2004 > String OR not working





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author String OR not working
Neil Aggarwal

2004-03-28, 11:38 pm

Hello:

I am trying to use the string OR in my regex pattern to allow multiple
possibilities for a string, but it is failing.

Take, for example, the string
"adsl-68-72-170-142.dsl.chcgil.ameritech.net"

When I apply this regex pattern: "\\.ameritech\\.net$"
I get a match

But, when I apply this regex pattern: "\\.(ameritech|sbc)\\.net$"
I do not get a match.

Any ideas why this is not working?

I am pasting my code below. I am using Fedora Core 1 with the gcc
that came with it.

Thanks,
Neil


#include <regex.h>
#include <stdio.h>
#include <stdlib.h>

/* The filter to match addresses */
regex_t PATTERN1;
regex_t PATTERN2;

/* Initialization */
void init() {
int status = regcomp(&PATTERN1,"\\.ameritech\\.net$",REG_ICASE);
if( status != 0 ) {
fprintf(stderr, "Could not compile regex pattern1\n");
exit(-1);
}
status = regcomp(&PATTERN2,"\\.(ameritech|sbc)\\.net$",REG_ICASE);
if( status != 0 ) {
fprintf(stderr, "Could not compile regex pattern2\n");
exit(-1);
}
}

int main(argc, argv)
int argc;
char *argv[];
{
/* Initialize */
init();

char buffer[1024] = "adsl-68-72-170-142.dsl.chcgil.ameritech.net";

int status = regexec(&PATTERN1, buffer, (size_t) 0, NULL, 0);
if( status == REG_NOMATCH )
printf( "Did not match pattern 1\n" );
else
printf( "Matched pattern 1\n" );

status = regexec(&PATTERN2, buffer, (size_t) 0, NULL, 0);
if( status == REG_NOMATCH )
printf( "Did not match pattern 2\n" );
else
printf( "Matched pattern 2\n" );
}
Paul Pluzhnikov

2004-03-29, 1:38 am

neil@JAMMConsulting.com (Neil Aggarwal) writes:

> status = regcomp(&PATTERN2,"\\.(ameritech|sbc)\\.net$",REG_ICASE);


From 'man regex':

REG_EXTENDED
Use POSIX Extended Regular Expression syntax when
interpreting regex. If not set, POSIX Basic Regular
Expression syntax is used.

You need to turn this on for 'a|b' to work.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Sam Dennis

2004-03-29, 2:39 am

Neil Aggarwal wrote:
> I am trying to use the string OR in my regex pattern to allow multiple
> possibilities for a string, but it is failing.
>
> status = regcomp(&PATTERN2,"\\.(ameritech|sbc)\\.net$",REG_ICASE);


Alternation is no part of basic regular expressions; try
REG_ICASE|REG_EXTENDED instead.

> int main(argc, argv)
> int argc;
> char *argv[];


You really shouldn't be doing this these days; using it in a source file
that relies on C99 features looks utterly ridiculous, too. (Unless this
is meant to be C++... but there's still no reason for it.)

--
++acr@,ka"
Neil Aggarwal

2004-03-29, 11:36 am

Paul:

Thanks! That worked.


Neil.


Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> wrote in message
news:<m365co88dt.fsf@salmon.parasoft.com>...
> neil@JAMMConsulting.com (Neil Aggarwal) writes:
>
>
> From 'man regex':
>
> REG_EXTENDED
> Use POSIX Extended Regular Expression syntax when
> interpreting regex. If not set, POSIX Basic Regular
> Expression syntax is used.
>
> You need to turn this on for 'a|b' to work.
>
> Cheers,

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com