Regex match a string with spaces (use quotes?) in an if statement
I did not manage to inline the expression like this:
if [[ "$text" =~ "^ *This " ]]; then
but if you put the expression in a variable you could use normal regex syntax like this:
pat="^ *This "if [[ $text =~ $pat ]]; then
Note that the quoting on $text
and $pat
is unnessesary.
Edit:A convinient oneliner during the development:
pat="^ *This is "; [[ " This is just a test string" =~ $pat ]]; echo $?