regex : how to match word ending with parentheses “)”

前端 未结 3 1109
礼貌的吻别
礼貌的吻别 2020-12-22 08:32

I want to match string ending with \')\' . I use pattern :

 \"[)]\\b\" or \".*[)]\\b\"

It should match the string :

x=main2         


        
3条回答
  •  失恋的感觉
    2020-12-22 09:15

    The \b word boundary is ambiguous: after a word character, it requires that the next character must a non-word one or the end of string. When it stands after a non-word char (like )) it requires a word character (letter/digit/underscore) to appear right after it (not the end of the string here!).

    So, there are three solutions:

    • Use \B (a non-word boundary): .*[)]\B (see demo) that will not allow matching if the ) is followed with a word character
    • Use .*[)]$ with MULTILINE mode (add (?m) at the start of the pattern or add the /m modifier, see demo)
    • Emulate the multiline mode with an alternation group: .*[)](\r?\n|$) (see demo)

提交回复
热议问题