How to verify if the string also starts and ends with one or more space(s) ?
if [[ $username =~ [^0-9A-Za-z]+ ]]
(basically input should be alphanumeric, no spaces anywhere, even in the beginning or in the end, and no commas, underscores, hiphens etc)
The above regex unfortunately does NOT match leading & trailing spaces, but it matches spaces in between ?
Without awk, sed, is there any way I can fix the above regex to match leading & trailing spaces also ?
Thanks in advance
Bash regexes can be tricky with quoting: regex metachars must NOT be quoted, but, since whitespace is significant in the shell, spaces must be quoted. This regex matching a string beginning and ending with a space:
[[ $s =~ ^" ".*" "$ ]] && echo y
To test if a string contains a space, do one of:
[[ $s =~ [[:space:]] ]] && echo y
[[ $s == *" "* ]] && echo y
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With