Consider the string "AB 1 BA 2 AB 3 BA"
. How can I match the content between "AB"
and "BA"
in a non-greedy fashion (in awk)?
I have tried the following:
awk '
BEGIN {
str="AB 1 BA 2 AB 3 BA"
regex="AB([^B][^A]|B[^A]|[^B]A)*BA"
if (match(str,regex))
print substr(str,RSTART,RLENGTH)
}'
with no output. I believe the reason for no match is that there is an odd number of characters between "AB"
and "BA"
. If I replace str
with "AB 11 BA 22 AB 33 BA"
the regex seems to work..