正则表达式说什么不匹配?
我想知道如何在正则表达式中匹配除特定字符串(称为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我确信这是一个骗局。
一种方法是用这样的前瞻开始您的模式:
在任何值得关注的正则表达式系统中都可以这样编写:
现在只需将其放在模式的前面,然后在后添加您想要的任何其他内容。这就是表达逻辑
!/for/ && 的方式。 ⋯
当你必须将这些知识构建到模式中时。它类似于构造
/foo/ && 的方式。 /栏/ && /glarch/
当你必须将其放入单个模式时,即I’m sure this a dup.
One way is to start your pattern with a lookahead like this:
That can be written like this in any regex system worth bothering with:
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匹配除
for
之外的任何字符串。匹配任何不包含
for
的字符串。匹配任何不包含
for
作为完整单词但允许诸如forceps
之类的单词的字符串。matches any string except
for
.matches any string that doesn't contain
for
.matches any string that doesn't contain
for
as a complete word, but allows words likeforceps
.您可以尝试检查字符串是否与
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)