I can't get my REGEX Pattern below to print an error when, say for example the user accidentally inputs 2 "/" consecutively by accident...
My REGEX Pattern/Code is: *Notice the double "//" after "/usr"...
user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20"
MY_PATTERN="((/{1,1}[[:alnum:]_-]*)+).*$"
if [[ "$user_input" =~ $MY_PATTERN ]]
then
echo "TRUE"
else
echo "FALSE"
fi
So my question is how can I get this to print FALSE when the user enters a double "//"?
I thought the part of my REGEX that has "/{1,1}" would mean match one and ONLY one consecutive "/"...?
Just so I'm clear what that does...
Will that remove any consecutive "/" and replace it with only one "/" and save the input into a NEW variable, then compare
the NEW var with the original...?
Sounds like a plan to me... Cool, Thanks!
Thanks Again,
Matt
---------- Post updated at 03:49 PM ---------- Previous update was at 03:44 PM ----------
Hey Shamrock, thanks for the reply...
Bash does have a "=~" Operator... Isn't that the REGEX Operator?
From the Bash Manpage:
Thanks,
Matt
---------- Post updated at 04:32 PM ---------- Previous update was at 03:49 PM ----------
Hey bipinajith,
Will I get the same result with your sed command and this one below..?
### the "\/{2,}" will 2 or more consecutive forward slashes "/"
tmp_input=$(echo "$user_input" | sed 's/\/\{2,\}/\//g' )
Is there any real difference between yours and mine..?
Ok my bad...so bash has that operator in any case if you want to look for the presence of two consecutive slashes then your regexp must be formed accordingly since that is what you want to find. Your regexp is looking for a single slash and $user_input contains it so it wont report false. I modified the code a bit to suit what you need to find...