awk gensub got the wrong part of matches

后端 未结 1 1595
-上瘾入骨i
-上瘾入骨i 2021-01-26 23:26

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}\         


        
1条回答
  •  再見小時候
    2021-01-26 23:52

    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

    0 讨论(0)
提交回复
热议问题