匹配尽可能多的不使用前瞻

时间:2015-07-26 18:27:58

标签: c# .net regex

我想匹配维基列表#但不是*或者跟随#。 E.g。

# Something -> Match
#* Something -> Not match
## Something -> Match
##* Something -> Not matched
##: Something -> Not matched

我想出了以下正则表达式:

Regex.Match("##* Something", @"^#+(?![*:])\s*(?<Definition>.*?)\s*$").Groups["Definition"]

但定义值是'#* Something'。我发现(?![*:])导致#+只匹配一次,而不是像上面的例子中那样 - 有两个#,但只有第一个匹配。

如何编写模式以便匹配?

非常感谢

2 个答案:

答案 0 :(得分:1)

您需要修改前瞻:

@"^(?!#+[*:])#+\s*(?<Definition>.*?)\s*$"

Live Demo

答案 1 :(得分:0)

如果好的是&#34;#&#34;其次是空格:(然后不需要定义&#34; *&#34;或&#34;:&#34;)

^#+\s(?<Definition>.*?)\s*$

或者如果(1)字符可能跟随&#34;#&#34;这还不是空间,但不能是&#34;:&#34;或&#34; *&#34;。

^#+[^:\*]?\s(?<Definition>.*?)\s*$
相关问题