我的正则表达式以量词 * 结尾。 但是我在字符串中几乎没有匹配项。我怎样才能让它仍然找到所有匹配项?我的正则表达式:
((CMD1|CMD2)+(?::|;)+.*)
测试字符串为"cmd1: test.test.test cmd2: test2.test2.test2"
所以我需要匹配:
cmd1: test. test. test
cmd2: test2. test2. test2
命令可以是随机的单词,例如“Look”、“Take”、“Go”。一个字符串中可能出现 n 次任何命令。 示例:
Go: some sentences. and more. Take: other more sentences, and even more text here. Look: more and more. and more.
最佳答案
您可以使用积极的前瞻:
- 匹配一个单词字符一次或多次
\w+
- 匹配冒号
:
- 匹配任何字符零次或多次
.*
- 使其非贪婪
?
- 断言一个单词字符一次或多次
\w+
后跟冒号:
或|
(?=\w+:|$)
关于c# - 正则表达式:很少匹配 * 量词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47960042/