说明
^(?!.*?[Pp]*(OST|ost)*\.*\s*[Mm][Aa][Nn]\s*(\d.)*)[a-z0-9A-Z-]*$
** 要更好地查看图像,只需右键单击图像并选择在新窗口中查看
此正则表达式将执行以下操作:
- 不允许您的第一个表达式匹配字符串中的任何位置
- 然后将只允许
a-z、A-Z、0-9 和-
我想指出,如果你的字符串只允许字母、数字和连字符,那么你的表达式中的点测试是不必要的。也可以使用不区分大小写标志来删除多个大小写字符集。
示例
现场演示
https://regex101.com/r/oY0hK2/1
示例文本
aWoeed1#fde39393aii
aWoeed1fde39393AII
aWoeed1fde39393AIIpostman 3a
示例匹配
aWoeed1fde39393AII
说明
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
--------------------------------------------------------------------------------
[Pp]* any character of: 'P', 'p' (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \1 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
OST 'OST'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
ost 'ost'
--------------------------------------------------------------------------------
)* end of \1 (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in \1)
--------------------------------------------------------------------------------
\.* '.' (0 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
[Mm] any character of: 'M', 'm'
--------------------------------------------------------------------------------
[Aa] any character of: 'A', 'a'
--------------------------------------------------------------------------------
[Nn] any character of: 'N', 'n'
--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0
or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
( group and capture to \2 (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
\d digits (0-9)
--------------------------------------------------------------------------------
. any character except \n
--------------------------------------------------------------------------------
)* end of \2 (NOTE: because you are using a
quantifier on this capture, only the
LAST repetition of the captured pattern
will be stored in \2)
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
[a-z0-9A-Z-]* any character of: 'a' to 'z', '0' to '9',
'A' to 'Z', '-' (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string