Reputation: 1
Can some one help me to find a regular expression for the pattern below,
I am trying to use regular expression to match a patter in shell script.
The pattern could be a combination of 'ab' 'xy' 'ij' 'pqr', in any order and seperated by a comma ',' or only 'all'
Ex.
1) "ab,xy,ij,pqr" - valid
2) "ij,pqr" - valid
3) "all" - valid
4) "ij,ab," - invalid because it ends with a comma
5) "all,xy" - invalid because 'all' cannot be combined with xy ij pqr or ab
6) ",xy" - invalid because it starts with comma
7) "xy" - valid
Thank you.
@konsolebox @491243 @ajp15243 @Jerry Looks like I am doing something wrong, it works only for the RE regex4="(ab|xy|ij|pqr)(,(ab|xy|ij|pqr))*" so far and that too only if the string is like "ab,xy" just "ab" doesn't work. Here is what I have attempted so far:
#!/usr/bin/ksh
echo
echo echo $1
echo
regex2="^(all|(ab|xy|ij|pqr)(,(ab|xy|ij|pqr))*)$"
regex3="^(all|(ab|xy|ij|pqr)(,(ab|xy|ij|pqr))*)$"
regex4="(ab|xy|ij|pqr)(,(ab|xy|ij|pqr))*"
if [[ $1 == $regex2 ]]
then
echo "You got it again 22222222 !"
fi
if [[ $1 == $regex3 ]]
then
echo "You got it again 33333333 !"
fi
if [[ $1 == $regex4 ]]
then
echo "You got it again 44444444 !"
fi
Output:
$
$ test.ksh ab,xy
echo ab,xy
You got it again 44444444 !
$
$
$
$ test.ksh ab
echo ab
$
1:30 PST
Ok, had some improvement:
"((ab|xy|ij|pqr)|(ab|xy|ij|pqr)(,(ab|xy|ij|pqr))*)|all)$"
this one works when the input is "xy", "xy,ab" but it also treating "xy,ab,all" as valid input.
Upvotes: 0
Views: 1481
Reputation: 1
ok, looks like finially the below RE worked, thank you all for the help.
(all)|(ab|xy|ij|pqr)|(ab|xy|ij|pqr)(,(ab|xy|ij|pqr)*)
Upvotes: 0
Reputation: 263723
I think this will do it.
^(((ab|xy|ij|pqr)(,(ab|xy|ij|pqr))*)|all)$
Upvotes: 1