Skip to main content
 首页 » 编程设计

c#之: Must contain a numeric, 的正则表达式不包括 "SomeText1"

2024年02月03日28zhujiabin

我需要一个正则表达式模式(必须是单一模式)来匹配任何包含数字的文本,不包括特定文字(即“SomeText1”)。

我匹配任何包含数字部分的文本:

^.*[0-9]+.*$ 

但是我在排除特定文字时遇到了问题。

更新:这是针对 .NET Regex 的。

提前致谢。

请您参考如下方法:

作为冗长的正则表达式:

^              # Start of string 
(?=.*[0-9])    # Assert presence of at least one digit 
(?!SomeText1$) # Assert that the string is not "SomeText1" 
.*             # If so, then match any characters 
$              # until the end of the string 

如果您的正则表达式风格不支持这些:

^(?=.*[0-9])(?!SomeText1$).*$