为什么需要否定正则表达式? [英] Why is negation of a regex needed?

查看:43
本文介绍了为什么需要否定正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于regex-negation 的问题太多了.

There are so many questions on regex-negation here on SO.

我不确定我是否理解为什么人们觉得需要否定正则表达式.为什么不使用像 grep -v 这样的东西,它只显示与正则表达式不匹配的结果?

I am not sure I understand why people feel the need to negate a regex. Why not use something like grep -v that shows only the results that do not match the regex?

$ ls
april  august  december  february  january  july  june  march  may  november  october  september
$ ls | grep ber
december
november
october
september
$ ls | grep -v ber
april
august
february
january
july
june
march
may

推荐答案

可能是因为 grep 不是唯一使用正则表达式的地方?它适用于这个简单的场景......实际上在许多其他情况下你可以说匹配这个正则表达式"......但是......好吧,如果你只需要否定怎么办正则表达式的一部分?匹配这个,但不匹配这个"你会怎么做?你不能否定整件事.

Probably because grep isn't the only place that regexes are used? It works in this simple scenario... and actually in many others where you can just say "doesn't match this regex"... but... well, what if you need to negate only part of a regex? "Matches this, but doesn't match this" how would you do that? You can't just negate the whole thing.

这篇关于为什么需要否定正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆