帮助处理特定的正则表达式 - 不包含某些字符串
在正则表达式中怎么说:
以大写字母开头的字符串的任何部分,包含至少一个空格字符,不包含字符串“_”
(空格下划线空格),并以字符串“!!!”结尾(不带引号)?
我在“不包含”部分遇到麻烦。
这是我到目前为止所得到的:
[A-Z].* .*!!!
如何修改它以指定“不包含'_'”?
它不需要是特定的字符串“_”。我怎么能说“不包含”任何字符串?例如不包含“狗”?
编辑:我希望解决方案与 Php 的“preg_replace”兼容
编辑:示例:
“_”示例:
Abc xyz! !! <---匹配
Hello World!!! <---匹配
有_空格下划线空格!!! <--- 不匹配
“狗”的示例:
多棒的狗!!! <--- 不匹配,(包含“狗”)
Hello World!!! <--- 匹配
How do I say, in regular expressions:
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.
Here is what I have so far:
[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"?
Edit: I'd like the solution to be compatible with Php's "preg_replace"
Edit: Examples:
Examples for " _ ":
Abc xyz!!! <---Matches
Hello World!!! <---Matches
Has _ Space Underscore Space!!! <--- Does Not Match
Examples for "dog":
What a dog!!! <--- Does not match, (contains "dog")
Hello World!!! <--- Matches
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
仅当
x(?!y)
表达式后面没有紧跟y
时,它才匹配x
。所以,这似乎是您想要的:其中
%s
是您的禁止字符串。The
x(?!y)
expression matchesx
only if it is not immediately followed byy
. So, this seems to be the thing you want:Where
%s
is your forbidden string.任何可能的正则表达式可能比两个正则表达式复杂得多。一个像你的:
[AZ].* .*!!!
,第二个应用于匹配的字符串并检查是否包含_
。Any possible regex for this would be probably much more complicated than two regexes. One like yours:
[A-Z].* .*!!!
and the second applied on matched strings and checking whether_
is contained.以任意大写开头;有一个可选字符串,其中包含除下划线之外的所有内容、空格,然后再次包含除下划线之外的所有内容,后跟三个感叹号。
Start with Any capital; have an optional string of everything except an underscore, a space, and then the everything except underscore again, followed by three exclamation marks.
首先测试字符串中是否出现“_”,因为这是不匹配的。然后检查你想要什么。
这就是我要做的,而不是花费大量时间试图找出一个正则表达式。
这是一个测试表达式的好网站: Nregex
编辑:我读了一些更多关于你的问题,发现我的答案并不是很好,所以这是另一次尝试。上述表达式之一的修改:
First test your string for any occurance of " _ ", since that is a no match. Then check for what you want.
That's what I would do instead of spending a lot of time trying to figure out one regular expression for it.
Here's a nice site for testing your expressions: Nregex
Edit: I read some more on your question and see that my answer wasn't really good, so here's another attempt. A modification of one of the expressions above:
编辑:我错过了您对至少一个空间的要求。以下正则表达式包含该要求:
Edit: I missed your requirement for at least one space. The below regex includes that requirement:
我曾经通过
grep
的-v
选项来解决它(如果我在 Linux 上和/或可以使用grep
)。所以搜索一些东西,然后跳过不感兴趣的部分。
没有 grep (该死的 Windows,没有管理员权限),但使用 Vim:(
这将打开 ,然后删除与您需要的内容不匹配的每一行,然后删除您不需要的每一行,然后将结果保存为
checkoutput你应该检查一下。)
HTH
I used to resolve it via
grep
's-v
option (if I'm on Linux and/or can usegrep
).So search for something, then skip the uninteresting parts.
Without grep (damn windows, without admin rights) but with Vim:
(This opens , then deletes every line what does not match what you need, then deletes every line, what you does not need, then save the results as
checkoutput
. Which you should check.)HTH
有一个很好的小程序,您可以在其中构建正则表达式并对其进行测试。
http://software.marioschneider-online.de/?C%23%3A_RegEx_Test
There is a nice little program in which you can built your regex together with testing it.
http://software.marioschneider-online.de/?C%23%3A_RegEx_Test