The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
regex test in bash subin_bala Shell Programming and Scripting 1 04-16-2008 12:27 AM
test command looks strange... jjinno Shell Programming and Scripting 2 11-02-2007 03:31 PM
Help regarding Error message: A test command parameter is not valid skyineyes Shell Programming and Scripting 5 06-29-2007 02:38 AM
a strange message when executing the sort command marwan UNIX for Dummies Questions & Answers 3 04-27-2007 04:32 AM
ed strange error message frenki Shell Programming and Scripting 6 10-29-2004 03:00 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-31-2006
Registered User
 

Join Date: May 2006
Posts: 2
Strange error message with regex test...

Hi all,

I have a script where i need to check the format of a string.
finally, i'm waiting a "process name" and 2 numbers separated by a ","

string like : "this_is_a_string.txt,1,10 should be ok"
string ok : "apache.exe,1,10"
string ok : "mysqld,50,0"

string not ok : "ap ache,1,10"
string not ok : "ap,ache,1,10"
string not ok : " apache,1,10"

PS : "" characters are only here to be able to show a string beginning with a space...

Here is what i tryed :

if [[ $(expr "$PROCESS_STRING" : '^[a-z0-9\-\.\_]*,[0-9]*,[0-9]*$') -ne 0 ]];
then
echo " : String OK"
else
echo " : String KO"
fi

But i always get an error :

expr: WARNING: BRE not portable: « ^[a-z0-9\-\.\_]*,[0-9]*,[0-9]*$ »: use of « ^ » character as first character of a base regular expression isn't portable; canceled.
: Format OK

The problem seems to be the "^" character at the beginning of my regex, but if i remove it, a string like " apache,1,10" would be ok but it shouldn't in my case...

thanks for your help

Florent
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-31-2006
tayyabq8's Avatar
UNIX Hobbyist
 

Join Date: Nov 2004
Location: /World/Asia/MiddleEast/Kuwait/Salmiya
Posts: 513
Try the same code without "^" in ksh
Code:
if [[ $(expr "$PROCESS_STRING" : '[a-z0-9\-\.\_]*,[0-9]*,[0-9]*$') -ne 0 ]];
it shouldn't match any string with a whitespace like "ap ache,1,9" or " apache,1,9"

Regards,
Tayyab
Reply With Quote
  #3 (permalink)  
Old 05-31-2006
Registered User
 

Join Date: May 2006
Posts: 2
sorry, it matches if space is the first character of the string...
Reply With Quote
  #4 (permalink)  
Old 05-31-2006
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Quote:
Originally Posted by fgilain
Hi all,

I have a script where i need to check the format of a string.
finally, i'm waiting a "process name" and 2 numbers separated by a ","

string like : "this_is_a_string.txt,1,10 should be ok"
string ok : "apache.exe,1,10"
string ok : "mysqld,50,0"

string not ok : "ap ache,1,10"
string not ok : "ap,ache,1,10"
string not ok : " apache,1,10"

PS : "" characters are only here to be able to show a string beginning with a space...

Here is what i tryed :

if [[ $(expr "$PROCESS_STRING" : '^[a-z0-9\-\.\_]*,[0-9]*,[0-9]*$') -ne 0 ]];
then
echo " : String OK"
else
echo " : String KO"
fi

But i always get an error :

expr: WARNING: BRE not portable: « ^[a-z0-9\-\.\_]*,[0-9]*,[0-9]*$ »: use of « ^ » character as first character of a base regular expression isn't portable; canceled.
: Format OK

The problem seems to be the "^" character at the beginning of my regex, but if i remove it, a string like " apache,1,10" would be ok but it shouldn't in my case...

thanks for your help

Florent
What OS are you on?
It works fine with Solaris' /usr/bin/expr and /usr/xpg4/bin/expr
Reply With Quote
  #5 (permalink)  
Old 05-31-2006
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,226
Using expr STRING : REGEXP performs an anchored pattern match of REGEXP in STRING. So you shouldn't need to anchor your expression with '^' or '$'.
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:43 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0