Skip to main content
 首页 » 编程设计

c#之如何不匹配双空格

2024年02月23日18java哥

到目前为止一切顺利,但我想匹配 "Health and Beauty"
而不是 "Health and Beauty _ _ _"(尾随 6空格)

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#为例)

请您参考如下方法:

更改 @"^(?<dept>.*)"是非贪婪的,并匹配之后的额外空白:

@"^(?<dept>.*?)\s*"