我正在寻找具有以下细节的正则表达式模式
-
不区分大小写
-
一次只有1个字母有效
-
字母前面带有可选空格的数字
-
任何符号均有效,即
%
,@
,/
,-
等 -
它不应该匹配3个整数
2342 34534 2342
. 但345345 / 435
或236 545
没问题 -
模式需要匹配完整的字符串
-
多个空格和下划线将在事先清理并且不相关
Matches
"1"
"12"
"12 34"
"12 a 34"
"1a"
"54b"
"32 c"
"43-23"
"45 c/34"
"45/34 d"
"67 / 345"
"23a / 56"
"12 / 56B"
只是为了更具体 . 以下内容不应匹配
"1 3 5"
"34 a a"
"34b s"
"56/v"
"234 / a"
"234a/a"
"34b 456s"
"34b/456s"
"34b / 456s"
我目前的尝试是 ^\d+[\s]?[a-z\W]?[\s]?[\W]?[\s]?\d+?[a-z]?$
尽管生病很可能需要不止一种模式(对于符号两边的字母),我的主要问题是试图使 ?
正常工作,即当前模式id期望获得更多正面
Update
当前正则表达式产生以下结果 .
Good list
-------------
False - 1 <= this should match
True - 12 34
True - 12 a 34
False - 1a <= this should match
True - 54b
False - 32 c <= this should match
True - 43-23
True - 45c/34
True - 45/34d
True - 67 / 345
True - 23a / 56
True - 12 / 56b
Bad list
-------------
False - 1 3 5
False - 34 a a
False - 34b s
False - 234a/a
True - 34b 456s <= these are false positives only because they have 2 letters
True - 34b/456s
True - 34b / 456s
忽略误报,我可以通过创建2个模式来修复,我坚持为什么当前的正则表达式与指出的那些不匹配
2 回答
根据您的描述猜测,我建议采用以下模式:
Demo
示例代码:
你也可以尝试一下 .
Demo in .NET regex tester ,,,请点击标签,
[table]
和[context]
了解更多匹配信息 .