特定正则表达式的帮助 - 不包含某些字符串 [英] Help With Particular Regular Expression - Not Containing Some String
问题描述
用正则表达式怎么说:
以大写字母开头的字符串的任何部分,至少包含一个空格字符,不包含字符串" _ "
(空格下划线空格),并以字符串!!!"结尾(没有引号)?
Any portion of a string beginning with a capital letter, containing at least one space character, not containing the string
" _ "
(space underscore space), and ending with the string "!!!" (without the quotes)?
我在处理不包含"部分时遇到问题.
I am having trouble with the "not containing" part.
这是我目前所拥有的:
[A-Z].* .*!!!
如何修改它以同时指定不包含‘_’"?
How do I modify this to also specify "Not containing ' _ '"?
它不需要是特定的字符串_".我怎么能说不包含"任何字符串?例如不包含狗"?
It does not need to be the specific string " _ ". How can I say "not containing" ANY string? For instance not containing "dog"?
编辑:我希望解决方案与 Php 的preg_replace"兼容
Edit: I'd like the solution to be compatible with Php's "preg_replace"
编辑:示例:
_"的示例:
ABC xyz!!!<---匹配
Abc xyz!!! <---Matches
世界你好!!!<---匹配
Hello World!!! <---Matches
有_空格下划线空格!!!<--- 不匹配
Has _ Space Underscore Space!!! <--- Does Not Match
狗"的示例:
什么狗!!!<--- 不匹配,(包含狗")
What a dog!!! <--- Does not match, (contains "dog")
世界你好!!!<--- 匹配
Hello World!!! <--- Matches
推荐答案
x(?!y)
表达式仅在后面没有紧跟 x
时才匹配 x
代码>y.所以,这似乎是你想要的:
The x(?!y)
expression matches x
only if it is not immediately followed by y
. So, this seems to be the thing you want:
[A-Z](?!%s)(.(?!%s))* (.(?!%s))*!!!
其中 %s
是您的禁止字符串.
Where %s
is your forbidden string.
这篇关于特定正则表达式的帮助 - 不包含某些字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!