帮助处理特定的正则表达式 - 不包含某些字符串

发布于 2024-09-28 05:56:42 字数 581 浏览 3 评论 0原文

在正则表达式中怎么说:

以大写字母开头的字符串的任何部分,包含至少一个空格字符,不包含字符串
“_”(空格下划线空格),并以字符串“!!!”结尾(不带引号)?

我在“不包含”部分遇到麻烦。

这是我到目前为止所得到的:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

逆蝶 2024-10-05 05:56:42

仅当 x(?!y) 表达式后面没有紧跟 y 时,它才匹配 x。所以,这似乎是您想要的:

[A-Z](?!%s)(.(?!%s))* (.(?!%s))*!!!

其中 %s 是您的禁止字符串。

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))*!!!

Where %s is your forbidden string.

等往事风中吹 2024-10-05 05:56:42

任何可能的正则表达式可能比两个正则表达式复杂得多。一个像你的: [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.

2024-10-05 05:56:42

以任意大写开头;有一个可选字符串,其中包含除下划线之外的所有内容、空格,然后再次包含除下划线之外的所有内容,后跟三个感叹号。

[A-Z][^_]*[ ][^_]*!!!

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.

[A-Z][^_]*[ ][^_]*!!!
阳光①夏 2024-10-05 05:56:42

首先测试字符串中是否出现“_”,因为这是不匹配的。然后检查你想要什么。

这就是我要做的,而不是花费大量时间试图找出一个正则表达式。

这是一个测试表达式的好网站: Nregex

编辑:我读了一些更多关于你的问题,发现我的答案并不是很好,所以这是另一次尝试。上述表达式之一的修改:

[A-Z](?! _ )(\w(?! _ ))* (\w(?! _ ))*!!!

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:

[A-Z](?! _ )(\w(?! _ ))* (\w(?! _ ))*!!!
千秋岁 2024-10-05 05:56:42
[A-Z]([^ ]*(?! _ ) ?)*!!!

编辑:我错过了您对至少一个空间的要求。以下正则表达式包含该要求:

[A-Z][^ ]* (?!_ )((?! _ ).)*!!!
[A-Z]([^ ]*(?! _ ) ?)*!!!

Edit: I missed your requirement for at least one space. The below regex includes that requirement:

[A-Z][^ ]* (?!_ )((?! _ ).)*!!!
澜川若宁 2024-10-05 05:56:42

我曾经通过 grep-v 选项来解决它(如果我在 Linux 上和/或可以使用 grep)。

所以搜索一些东西,然后跳过不感兴趣的部分。

grep something <INPUT> | grep -v uninteresting

没有 grep (该死的 Windows,没有管理员权限),但使用 Vim:(

vim -c "v/i'm searching for this/d" -c "g/and don't need this/d" -c "w checkoutput" <INPUT>

这将打开 ,然后删除与您需要的内容不匹配的每一行,然后删除您不需要的每一行,然后将结果保存为 checkoutput你应该检查一下。)

HTH

I used to resolve it via grep's -v option (if I'm on Linux and/or can use grep).

So search for something, then skip the uninteresting parts.

grep something <INPUT> | grep -v uninteresting

Without grep (damn windows, without admin rights) but with Vim:

vim -c "v/i'm searching for this/d" -c "g/and don't need this/d" -c "w checkoutput" <INPUT>

(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

东风软 2024-10-05 05:56:42

有一个很好的小程序,您可以在其中构建正则表达式并对其进行测试。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文