我的正则表达式以量词 * 结尾。 但是我在字符串中几乎没有匹配项。我怎样才能让它仍然找到所有匹配项?我的正则表达式:
((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+:|$)
