我知道可以匹配一个单词,然后用其他工具逆转比赛(例如 grep -v)。但是,可以匹配不包含一个特定的单词,例如 hede,使用常规表达式的线条吗?
入口:
hoho
hihi
haha
hede
代码:
grep "<Regex for 'doesn't contain hede'>" input
所需的产量:
hoho
hihi
haha
我知道可以匹配一个单词,然后用其他工具逆转比赛(例如 grep -v)。但是,可以匹配不包含一个特定的单词,例如 hede,使用常规表达式的线条吗?
入口:
hoho
hihi
haha
hede
代码:
grep "<Regex for 'doesn't contain hede'>" input
所需的产量:
hoho
hihi
haha
当前回答
此前提到的(?(?!)*是很棒的,因为它可以被 anchored。
^(?:(?!hede).)*$ # A line without hede
foo(?:(?!hede).)*bar # foo followed by bar, without hede between them
但在这种情况下,以下几点就足够了:
^(?!.*hede) # A line without hede
此简化已准备好添加“和”条款:
^(?!.*hede)(?=.*foo)(?=.*bar) # A line with foo and bar, but without hede
^(?!.*hede)(?=.*foo).*bar # Same
其他回答
不是雷格斯,但我发现使用带管的序列粘贴是合乎逻辑和有用的,以消除噪音。
例如,搜索一个 Apache 配置文件,没有所有评论 -
grep -v '\#' /opt/lampp/etc/httpd.conf # this gives all the non-comment lines
和
grep -v '\#' /opt/lampp/etc/httpd.conf | grep -i dir
序列格雷普的逻辑是(不是一个评论)和(比赛是)
用此,你避免在每个位置测试一个 lookahead:
/^(?:[^h]+|h++(?!ede))*+$/
相当于(为.net ):
^(?>(?:[^h]+|h+(?!ede))*)$
老答案:
/^(?>[^h]+|h+(?!ede))*$/
使用 ConyEdit,您可以使用命令行 cc.gl!/hede/ 获取不包含 regex 匹配的行,或者使用命令行 cc.dl /hede/ 删除包含 regex 匹配的行。
答案:
^((?!hede).)*$
解释:
^ 序列的开始,(组和捕获到 \1 (0 或更多次(匹配尽可能多的数量)),(?! 向前看,看看是否存在,
把你的绳子,
(注:因为你在捕获中使用量子,只有捕获模式的最后重复将存储在1美元之前的选项,而字符的结尾。
如果你想匹配一个字符,否认类似于否认字符类的单词:
例如,一条线:
<?
$str="aaa bbb4 aaa bbb7";
?>
不要使用:
<?
preg_match('/aaa[^bbb]+?bbb7/s', $str, $matches);
?>
使用:
<?
preg_match('/aaa(?:(?!bbb).)+?bbb7/s', $str, $matches);
?>
警告“(?!bbb.)”不是 lookbehind 或 lookahead,它是 lookcurrent,例如:
"(?=abc)abcde", "(?!abc)abcde"