正则表达式:通过排除匹配,没有前瞻 - 有可能吗? [英] Regex: Matching by exclusion, without look-ahead - is it possible?
问题描述
在某些正则表达式风格中,不支持 [否定] 零宽度断言(前瞻/后视).
In some regex flavors, [negative] zero-width assertions (look-ahead/look-behind) are not supported.
这使得声明排除非常困难(不可能?).例如没有的每一行都有foo",就像这样:
This makes it extremely difficult (impossible?) to state an exclusion. For example "every line that does not have "foo" on it", like this:
^((?!foo).)*$
完全不使用环视是否可以实现同样的事情(暂时搁置复杂性和性能问题)?
Can the same thing be achieved without using look-around at all (complexity and performance concerns set aside for the moment)?
推荐答案
UPDATE: 它失败了在 oo 之前有两个 ff"作为 @Ciantic 在评论中指出.
UPDATE: It fails "with two ff before oo" as @Ciantic pointed out in the comments.
^(f(o[^o]|[^o])|[^f])*$
注意:在客户端否定匹配比使用上面的正则表达式要容易得多.
NOTE: It is much much easier just to negate a match on the client side instead of using the above regex.
正则表达式假设每一行都以换行符结尾,如果不是,请查看 C++ 和 grep 的正则表达式.
The regex assumes that each line ends with a newline char if it is not then see C++'s and grep's regexs.
Perl、Python、C++ 和 grep
中的示例程序都给出相同的输出.
Sample programs in Perl, Python, C++, and grep
all give the same output.
#!/usr/bin/perl -wn
print if /^(f(o[^o]|[^o])|[^f])*$/;
#!/usr/bin/env python
import fileinput, re, sys
from itertools import ifilter
re_not_foo = re.compile(r"^(f(o[^o]|[^o])|[^f])*$")
for line in ifilter(re_not_foo.match, fileinput.input()):
sys.stdout.write(line)
c++
c++
#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main()
{
boost::regex re("^(f(o([^o]|$)|([^o]|$))|[^f])*$");
//NOTE: "|$"s are there due to `getline()` strips newline char
std::string line;
while (std::getline(std::cin, line))
if (boost::regex_match(line, re))
std::cout << line << std::endl;
}
$ grep "^\(f\(o\([^o]\|$\)\|\([^o]\|$\)\)\|[^f]\)*$" in.txt
示例文件:
foo
'foo'
abdfoode
abdfode
abdfde
abcde
f
fo
foo
fooo
ofooa
ofo
ofoo
输出:
abdfode
abdfde
abcde
f
fo
ofo
这篇关于正则表达式:通过排除匹配,没有前瞻 - 有可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!