Unix Technical Forum

Regarding regular expressions in Solaris

This is a discussion on Regarding regular expressions in Solaris within the comp.unix.solaris forums, part of the Solaris Operating System category; --> Hi, Am writing one C program for one of my module and facing one problem with the regular expression ...


Go Back   Unix Technical Forum > Unix Operating Systems > Solaris Operating System > comp.unix.solaris

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-12-2008, 06:03 AM
sunil
 
Posts: n/a
Default Regarding regular expressions in Solaris

Hi,

Am writing one C program for one of my module and facing one problem
with the regular expression functions provided by the library libgen.h
in solaris.

In this library we are having two functions to deal with
the regular expressions

char *regcmp(const char *string1, /* char *string2 */ ,
int /*(char*)0*/); ------this function is to compile the regular
expression.In case of failure it will return NULL.

char *regex(const char *re, const char *subject, /*
char *ret0 */ ...);---------This function is used to execute the
compiled expression against the subject string.


Problem

Compiled and executed the following regular expression
using the above two function like below.

char *name = regcmp("[A-Z]*",(char*)0);
const char *newcursor = regex(name,"ps");

In the above case written the regular expression only to check
whether the given value is capitals or not. To check this one I
submitted the value "ps" in lower case. Still the regular expression
is executing succefully wit out failure.In the above case it shold
have to fail but still it is successful. The regex function is not
returning NULL in this case.

If anybody knows abot this please let me know.


Regards
Sunil.




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-12-2008, 06:03 AM
Chris Ridd
 
Posts: n/a
Default Re: Regarding regular expressions in Solaris

On 2007-12-13 07:46:44 +0000, sunil <sunil.vvn@gmail.com> said:

> Hi,
>
> Am writing one C program for one of my module and facing one problem
> with the regular expression functions provided by the library libgen.h
> in solaris.
>
> In this library we are having two functions to deal with
> the regular expressions
>
> char *regcmp(const char *string1, /* char *string2 */ ,
> int /*(char*)0*/); ------this function is to compile the regular
> expression.In case of failure it will return NULL.
>
> char *regex(const char *re, const char *subject, /*
> char *ret0 */ ...);---------This function is used to execute the
> compiled expression against the subject string.
>
>
> Problem
>
> Compiled and executed the following regular expression
> using the above two function like below.
>
> char *name = regcmp("[A-Z]*",(char*)0);
> const char *newcursor = regex(name,"ps");
>
> In the above case written the regular expression only to check
> whether the given value is capitals or not. To check this one I
> submitted the value "ps" in lower case. Still the regular expression
> is executing succefully wit out failure.In the above case it shold
> have to fail but still it is successful. The regex function is not
> returning NULL in this case.


The expression "[A-Z]*" will also match, because the * means *0* or
more of the preceding expression. Zero upper-case characters matches a
lower-case string :-)

You should read regexp(5) (as recommended by the regcmp(1) man page)
and regex(5). A cursory glance suggests that anchoring will help:
"^[A-Z]+$"

Cheers,

Chris

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-12-2008, 06:03 AM
Wayne
 
Posts: n/a
Default Re: Regarding regular expressions in Solaris

sunil wrote:
> Hi,
>
> Am writing one C program for one of my module and facing one problem
> with the regular expression functions provided by the library libgen.h
> in solaris.
>
> In this library we are having two functions to deal with
> the regular expressions
>
> char *regcmp(const char *string1, /* char *string2 */ ,
> int /*(char*)0*/); ------this function is to compile the regular
> expression.In case of failure it will return NULL.
>
> char *regex(const char *re, const char *subject, /*
> char *ret0 */ ...);---------This function is used to execute the
> compiled expression against the subject string.
>
>
> Problem
>
> Compiled and executed the following regular expression
> using the above two function like below.
>
> char *name = regcmp("[A-Z]*",(char*)0);
> const char *newcursor = regex(name,"ps");
>
> In the above case written the regular expression only to check
> whether the given value is capitals or not. To check this one I
> submitted the value "ps" in lower case. Still the regular expression
> is executing succefully wit out failure.In the above case it shold
> have to fail but still it is successful. The regex function is not
> returning NULL in this case.
>
> If anybody knows abot this please let me know.
>
>
> Regards
> Sunil.


"[A-Z]* matches zero or more occurrences of a capital letter. Thus
you got the correct result! You should try a more specific RE, such
as "^[A-Z][A-Z]*$", or (depending on your RE dialect) "^[A-Z]+$". If
a zero length string should return true use "^[A-Z]*$".

Ranges such as "[A-Z]" are only guaranteed to work if the locale
is set to "C" (or "POSIX", which is the same). (I ran into problems
when my locale was set to en_US.) The proper POSIX RE is:
"^[[:upper:]]*$", which will match an empty string too.

-Wayne
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:11 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com