Bash regex irregularities
I see highlighting information about regex, but can't seem to find a solution to my problem. Hopefully this hasn't been covered earlier. As usual, not everyone is interested in getting a solution to me, but rather understands why it works the way it does. This is the only way I can learn bash and all this complexity.
My example:
#!/bin/bash
Stext="Hallo World"
re="(.[a-z]*)$"
[[ $Stext =~ $re ]]
DBTable=${BASH_REMATCH[1]}
echo $DBTable
DBTable=`expr "$Stext" : $re`
echo $DBTable
as most of you can guess, I don't get World twice and I don't know why.
So far, I've managed to get any answer from the expr version escaped:
DBTable=`expr "$Stext" : '\(.[a-z]*\)$'`
This still does not give the expected result.
Can someone give me some insight on this please.
source to share
expr
treats the regex as an implicit pinning at the beginning of the line - that is, it implicitly adds ^
- so you need to provide everything up to the point of interest. Also, as you noticed, you need to write \(
and \)
, not (
and )
, since it expr
uses Basic Regular Expressions (BRE) and not Extended Regular Expressions (ERE). So this:
expr 'Hallo World' : '.[a-z]* \(.[a-z]*\)$'
will print World
.
source to share