String OR not working
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > String OR not working




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    String OR not working  
Neil Aggarwal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-29-04 04:38 AM

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" );
}





[ Post a follow-up to this message ]



    Re: String OR not working  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-29-04 06: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.





[ Post a follow-up to this message ]



    Re: String OR not working  
Sam Dennis


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-29-04 07: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"





[ Post a follow-up to this message ]



    Re: String OR not working  
Neil Aggarwal


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
03-29-04 04:36 PM

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,





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:36 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register