正则表达式只捕获第一场比赛

Wan*_*hir 7 regex lookaround regex-lookarounds

我一直在谷歌上搜索堆栈溢出提供的推荐问题.但是,我没有找到答案.

我想用正则表达式解析一个字符串,字符串的例子是

Lot: He said: Thou shalt not pass!

我想Lot作为一个群体捕获,并且He said: Thou shalt not pass!.但是,当我使用我的(.+): (.+)模式时,它会返回

Lot: He said:Thou shalt not pass!

是否可以He said: Thou shalt not pass使用正则表达式捕获?

sp0*_*00m 21

你需要第一组的非贪婪(或懒惰)基数:(.+?): (.+).

关于http://www.regular-expressions.info/repeat.html,"懒惰而不是贪婪"一章的更多细节.


Ken*_*ent 5

试试这个:

([^:]+):\s*(.*)
Run Code Online (Sandbox Code Playgroud)