簡體   English   中英

正則表達式捕獲組提前終止

[英]Regex capturing group terminating early

我通常不問愚蠢的正則表達式問題。 但這是一個用於正則表達式的禿鷹。

拿這些弦...

-This is nice (show cow)
*This (or that) (show mouse)
-Whatever
-Hiya (everyone)

所有這些都應該匹配。 即以*-開頭,然后是任何文本,然后可以有條件地后面跟空格和(show xxx) ,其中xxx可以是任何文本。 我希望將這些單獨捕獲,即...

-This is nice (show cow)
   Match 1 : 'This is nice'
   Match 2 : '(show cow)' or just 'cow'

*This (or that) (show mouse)
   Match 1 : 'This (or that)'
   Match 2 : 'show mouse' or just 'mouse'

-Whatever
   Match 1 : 'Whatever'

-Hiya (everyone)
   Match 1 : 'Hiya (everyone)'

+Hello
   No Match

我嘗試了各種...

[\-*](.+?)(\s+\(show (.+)\))?
    Terminates early and only captures 1 char of match 1

[\-*](.+)(\s+\(show (.+)\))?
    Terminates late and captures all of the text as match 1 including `(show....`

[\-*](.+)(\s+\(show (.+)\)$)?
    As above

我相信這應該真的很簡單! 我不想進行否定(show在Match 1中(show -如我所願,例如-Hello (show a) (show b) ,其中Match 1是Hello (show a)而Match 2是(show b) (或只是b

我想你想要這樣的東西

^[-*](.*?)(?: \(show\s*([^)]*)\)|$)

DEMO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM