Reputation: 364
I'm trying to create a regex pattern in korn shell. I'm new to this korn shell scripting and its regex.
The regex is needed for the following case:
The allowed values
are as below:
80,01,00
80,00
01
00,80,01,02
The not allowed values
are as below:
80, --> with comma in the end.
,01, --> comma at the start and end.
,00 --> comma at the start.
8 --> with only one digit
800 --> with three digits
I have tried the below regex pattern:
^(*([0-9][0-9][,])+([0-9][0-9])*([,][0-9][0-9]))$
and the snippet from script is as below:
if [[ $END_POINT_LIST =~ ^(*([0-9][0-9][,])+([0-9][0-9])*([,][0-9][0-9]))$ ]]; then
echo "Input validation passed!"
export RETURN_CODE=16
exit 16
else
echo "[FATAL] The parameter doesn't match the expected pattern."
export RETURN_CODE=16
exit 16
fi
;;
Above regex pattern is not working as expected.
I'm not able to get the mistake in the regex.
Any suggestions to point out the mistake will be of great help.
Thank you!
EDIT:
In addition to the accepted answer from @degant, I would like to mention the following.
The mistake in the regex being posted in the question is,
The characters * and +
are placed in the start of pattern-list.
If that is changed to place the * and +
after the pattern-list it works fine.
The below corrected regex works fine as well:
^(([0-9][0-9][,])*([0-9][0-9])+([,][0-9][0-9])*)$
Upvotes: 1
Views: 114
Reputation: 4981
Regex to match numbers separated by commas and ensure commas aren't present in the front or the end:
^\d{2}(,\d{2})*$
In case the shell doesn't support \d
and {2}
:
^[0-9][0-9](,[0-9][0-9])*$
EDIT: Suggestions as per @Doqnach
Upvotes: 3