到目前为止一切顺利,但我想匹配"Health and Beauty"
而不是"Health and Beauty _ _ _"
(尾随六个空格)
var a = Regex.Match("Health and Beauty 08/05/11 TO 08/11/11",
@"^(?<dept>.*)" +
@"(?<startdate>[0-9]{2}/[0-9]{2}/[0-9]{2})\s+TO\s+" +
@"(?<enddate>[0-9]{2}/[0-9]{2}/[0-9]{2})$").Dump();
我尝试了一个消极的期待,但是。*继续匹配一切。
@"^(?<dept>.*(?!(\s\s)))\s+" // should be "not followed by two spaces
我只有匹配和提取的选项,而不是替换(C#是一个例子。)
答案 0 :(得分:4)
将@"^(?<dept>.*)"
更改为非贪婪,并在以下内容后匹配额外的空格:
@"^(?<dept>.*?)\s*"