采取以下正则表达式:
P[0-9]{6}(\s|\.|,)
Run Code Online (Sandbox Code Playgroud)
这是为了检查一个字符串中以"P"开头的6位数字 - 大部分工作正常.
问题是,如果发现多个匹配,我们需要失败 - 这可能吗?
即,使以下屏幕截图中的文本4失败,但仍然保持所有其他失败/通过,如下所示:

(此RegEx正在SQL .net CLR中执行)
如果此工具使用的正则表达式引擎确实是.NET引擎,那么您可以使用
^(?:(?!P[0-9]{6}[\s.,]).)*P[0-9]{6}[\s.,](?:(?!P[0-9]{6}[\s.,]).)*$
Run Code Online (Sandbox Code Playgroud)
如果它是本机SQL引擎,那么您不能使用单个正则表达式匹配,因为这些引擎不支持环绕声断言.
说明:
^ # Start of string
(?: # Start of group which matches...
(?!P[0-9]{6}[\s.,]) # unless it's the start of Pnnnnnn...
. # any character
)* # any number of times
P[0-9]{6}[\s.,] # Now match Pnnnnnn exactly once
(?:(?!P[0-9]{6}[\s.,]).)* # Match anything but Pnnnnnn
$ # until the end of the string
Run Code Online (Sandbox Code Playgroud)
在regex101.com上测试它.
| 归档时间: |
|
| 查看次数: |
347 次 |
| 最近记录: |