正则表达式说什么不匹配?

发布于 2024-11-05 23:28:11 字数 94 浏览 1 评论 0原文

我想知道如何在正则表达式中匹配除特定字符串(称为“for”)之外的任何字符。

我在想也许是这样的:[^for]* - 但那不起作用。

I’m wondering how to match any characters except for a particular string (call it "for") in a regex.

I was thinking maybe it was something like this: [^for]* — except that that doesn’t work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

冬天的雪花 2024-11-12 23:28:11

我确信这是一个骗局。

一种方法是用这样的前瞻开始您的模式:

(?=\A(?s:(?!for).)*\z)

在任何值得关注的正则表达式系统中都可以这样编写:

(?x)            # turn /x mode on for commentary & spacing
(?=             # lookahead assertion; hence nonconsumptive
    \A          # beginning of string
    (?s:        # begin atomic group for later quantification
                        # enable /s mode so dot can cross lines    
        (?! for )       # lookahead negation: ain't no "for" here
        .               # but there is any one single code point
    )           # end of "for"-negated anything-dot
    *           # repeat that group zero or more times, greedily
    \z          # until we reach the very end of the string
)               # end of lookahead

现在只需将其放在模式的前面,然后在后添加您想要的任何其他内容。这就是表达逻辑 !/for/ && 的方式。 ⋯ 当你必须将这些知识构建到模式中时。

它类似于构造 /foo/ && 的方式。 /栏/ && /glarch/ 当你必须将其放入单个模式时,即

(?=\A(?s:.)*foo)(?=\A(?s:.)*bar)(?=\A(?s:.)*glarch)

I’m sure this a dup.

One way is to start your pattern with a lookahead like this:

(?=\A(?s:(?!for).)*\z)

That can be written like this in any regex system worth bothering with:

(?x)            # turn /x mode on for commentary & spacing
(?=             # lookahead assertion; hence nonconsumptive
    \A          # beginning of string
    (?s:        # begin atomic group for later quantification
                        # enable /s mode so dot can cross lines    
        (?! for )       # lookahead negation: ain't no "for" here
        .               # but there is any one single code point
    )           # end of "for"-negated anything-dot
    *           # repeat that group zero or more times, greedily
    \z          # until we reach the very end of the string
)               # end of lookahead

Now just put that in the front of your pattern, and add whatever else you’d like afterwords. That’s how you express the logic !/for/ && ⋯ when you have to built such knowledge into the pattern.

It is similar to how you construct /foo/ && /bar/ && /glarch/ when you have to put it in a single pattern, which is

(?=\A(?s:.)*foo)(?=\A(?s:.)*bar)(?=\A(?s:.)*glarch)
ぽ尐不点ル 2024-11-12 23:28:11
^(?!for$).*$

匹配除 for 之外的任何字符串。

^(?!.*for).*$

匹配任何不包含 for 的字符串。

^(?!.*\bfor\b).*$

匹配任何不包含 for 作为完整单词但允许诸如 forceps 之类的单词的字符串。

^(?!for$).*$

matches any string except for.

^(?!.*for).*$

matches any string that doesn't contain for.

^(?!.*\bfor\b).*$

matches any string that doesn't contain for as a complete word, but allows words like forceps.

谁把谁当真 2024-11-12 23:28:11

您可以尝试检查字符串是否与 for 匹配,并否定结果,无论您使用什么语言(例如 if (not $_ =~ m/for/) in珀尔)

You can try to check whether the string matches for, and negate the result, in whatever language you use (e.g. if (not $_ =~ m/for/) in Perl)

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