How to tell a RegEx to be greedy on an 'Or' Expression

后端 未结 2 1649
醉梦人生
醉梦人生 2021-01-28 09:36

Text:

[A]I\'m an example text [] But I want to be included [[]]
[A]I\'m another text without a second part []

Regex:

\\[A\\][\\         


        
2条回答
  •  醉话见心
    2021-01-28 10:33

    I have changed your regex (actually simpler) to do what you want:

    \[A\].*\[?\[\]\]?
    

    It starts by matching the '[A]', then matches any number of any characters (greedy) and finally one or two '[]'.

    Edit:

    This will prefer double Square brackets:

    \[A\].*(?:\[\[\]\]|\[\])
    

提交回复
热议问题