Non-greedy regular expression match for multicharacter delimiters in awk

前端 未结 3 1576
后悔当初
后悔当初 2020-12-20 00:14

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)?

3条回答
  •  旧巷少年郎
    2020-12-20 00:56

    The other answer didn't really answer: how to match non-greedily? Looks like it can't be done in (G)AWK. The manual says this:

    awk (and POSIX) regular expressions always match the leftmost, longest sequence of input characters that can match.

    https://www.gnu.org/software/gawk/manual/gawk.html#Leftmost-Longest

    And the whole manual doesn't contain the words "greedy" nor "lazy". It mentions Extended Regular Expressions, but for greedy matching you'd need Perl-Compatible Regular Expressions. So… no, can't be done.

提交回复
热议问题