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

Try the same code without "^" in ksh

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

sorry, it matches if space is the first character of the string...

What OS are you on?
It works fine with Solaris' /usr/bin/expr and /usr/xpg4/bin/expr


  1. a-z0-9\-\.\_ ↩︎

Using expr STRING : REGEXP performs an anchored pattern match of REGEXP in STRING. So you shouldn't need to anchor your expression with '^' or '$'.