I need a single-pass regex for unix grep that contains, say alpha, but does not contain beta.
我需要unix grep的单通道regex,它包含alpha,但不包含beta。
grep 'alpha' <> | grep -v 'beta'
5 个解决方案
#1
17
^((?!beta).)*alpha((?!beta).)*$
would do the trick I think.
^((? !β)。)*αβ(? !)。*我认为美元就可以做到。
#2
30
The other answers here show some ways you can contort different varieties of regex to do this, although I think it does turn out that the answer is, in general, “don’t do that”. Such regular expressions are much harder to read and probably slower to execute than just combining two regular expressions using the boolean logic of whatever language you are using. If you’re using the grep
command at a unix shell prompt, just pipe the results of one to the other:
这里的其他答案显示了一些方法,您可以通过扭曲不同类型的regex来实现这一点,尽管我认为最终的答案是,一般来说,“不要那样做”。与使用任何语言的布尔逻辑组合两个正则表达式相比,这种正则表达式的读取要困难得多,执行起来可能也要慢得多。如果在unix shell提示符下使用grep命令,只需将其中一个的结果传递到另一个:
grep "alpha" | grep -v "beta"
I use this kind of construct all the time to winnow down excessive results from grep
. If you have an idea of which result set will be smaller, put that one first in the pipeline to get the best performance, as the second command only has to process the output from the first, and not the entire input.
我一直使用这种结构来筛选grep的过度结果。如果您知道哪个结果集会更小,请将该结果集放在管道中以获得最佳性能,因为第二个命令只需要处理第一个的输出,而不是整个输入。
#3
23
Well as we're all posting answers, here it is in awk ;-)
我们都在发布答案,在这里,它在awk;-)
awk '/x/ && !/y/' infile
I hope this helps.
我希望这可以帮助。
#4
3
I'm pretty sure this isn't possible with true regular expressions. The [^y]*x[^y]*
example would match yxy, since the * allows zero or more non-y matches.
我很确定这对于真正的正则表达式来说是不可能的。(^ y)* x[^ y]*将匹配yxy例子,自从*允许零个或多个non-y匹配。
EDIT:
编辑:
Actually, this seems to work: ^[^y]*x[^y]*$
. It basically means "match any line that starts with zero or more non-y characters, then has an x, then ends with zero or more non-y characters".
实际上,这似乎工作:^(^ y)* x[^ y]*美元。它的基本意思是“匹配任何一行,从0或更多非y字符开始,然后有一个x,然后以0或更多非y字符结束”。
#5
0
Try using the excludes operator: [^y]*x[^y]*
试着用不包括操作符(^ y):* x[^ y]*
#1
17
^((?!beta).)*alpha((?!beta).)*$
would do the trick I think.
^((? !β)。)*αβ(? !)。*我认为美元就可以做到。
#2
30
The other answers here show some ways you can contort different varieties of regex to do this, although I think it does turn out that the answer is, in general, “don’t do that”. Such regular expressions are much harder to read and probably slower to execute than just combining two regular expressions using the boolean logic of whatever language you are using. If you’re using the grep
command at a unix shell prompt, just pipe the results of one to the other:
这里的其他答案显示了一些方法,您可以通过扭曲不同类型的regex来实现这一点,尽管我认为最终的答案是,一般来说,“不要那样做”。与使用任何语言的布尔逻辑组合两个正则表达式相比,这种正则表达式的读取要困难得多,执行起来可能也要慢得多。如果在unix shell提示符下使用grep命令,只需将其中一个的结果传递到另一个:
grep "alpha" | grep -v "beta"
I use this kind of construct all the time to winnow down excessive results from grep
. If you have an idea of which result set will be smaller, put that one first in the pipeline to get the best performance, as the second command only has to process the output from the first, and not the entire input.
我一直使用这种结构来筛选grep的过度结果。如果您知道哪个结果集会更小,请将该结果集放在管道中以获得最佳性能,因为第二个命令只需要处理第一个的输出,而不是整个输入。
#3
23
Well as we're all posting answers, here it is in awk ;-)
我们都在发布答案,在这里,它在awk;-)
awk '/x/ && !/y/' infile
I hope this helps.
我希望这可以帮助。
#4
3
I'm pretty sure this isn't possible with true regular expressions. The [^y]*x[^y]*
example would match yxy, since the * allows zero or more non-y matches.
我很确定这对于真正的正则表达式来说是不可能的。(^ y)* x[^ y]*将匹配yxy例子,自从*允许零个或多个non-y匹配。
EDIT:
编辑:
Actually, this seems to work: ^[^y]*x[^y]*$
. It basically means "match any line that starts with zero or more non-y characters, then has an x, then ends with zero or more non-y characters".
实际上,这似乎工作:^(^ y)* x[^ y]*美元。它的基本意思是“匹配任何一行,从0或更多非y字符开始,然后有一个x,然后以0或更多非y字符结束”。
#5
0
Try using the excludes operator: [^y]*x[^y]*
试着用不包括操作符(^ y):* x[^ y]*