I want to extract \"yy a1 b1\" with the awk expression
echo \"xx yy [a1 b1] / zz [a2 b2]/\" | awk \'{p=gensub(/.*\\[([a-z0-9 ]+)\\].*/,\"\\\\1\",1); print $2,p}\
The point is that the first .*
as many chars as possible, thus making the regex engine match the last occurrence of the remaining subpatterns.
You may use the following solution:
/[^][]*\[([a-z0-9 ]+)\].*/
where [^][]*
matches any 0 or more chars other than ]
and [
, thus, allowing the regex engine to stop right before the first [...]
See the online demo