我有一个下面的正则表达式,如果它找到PO票房组合,则返回true
\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b我想要的正好相反,如果一个特定的字符串有po票房的组合,那么它应该返回false,否则允许所有事情。
有人能帮我一下吗?
发布于 2011-04-02 07:18:47
// leon's p.o. box detection regex
// for better results, trim and compress whitespace first
var pobox_re = /^box[^a-z]|(p[-. ]?o.?[- ]?|post office )b(.|ox)/i,
arr = [
"po box",
"p.o.b.",
"p.o. box",
"po-box",
"p.o.-box",
"PO-Box",
"p.o box",
"pobox",
"p-o-box",
"p-o box",
"post office box",
"P.O. Box",
"PO Box",
"PO box",
"box 122",
"Box122",
"Box-122",
];
for (var i in arr)
console.log(pobox_re.test(arr[i]));发布于 2010-12-31 03:03:22
非常感谢你们的帮助,但我找到了解决方案
(?i:^(?!([\s|\0-9a-zA-Z. ,:/$&#'-]*|p[\s|\.|, ]*|post[\s|\.]*)(o[\s|\.|, ]*|office[\s|\. ]*)(box[\s|\. ]*))[0-9a-zA-Z. ,:/$&#'-]*$)发布于 2010-12-31 05:01:34
在去掉字符类中的|并删除一些不适当的转义后,我在Perl中尝试了您的正则表达式。看起来还可以,尽管有点负面(?!)。
use strict;
use warnings;
my $regex = qr/
(?i:
^
(?!
( [\s0-9a-zA-Z. ,:\$&#'-]*
| p[\s., ]*
| post[\s.]*
)
( o[\s., ]*
| office[\s. ]*
)
(
box[\s. ]*
)
)
[0-9a-zA-Z. ,:\$&#'-]*
$
) /x;
my @tests = (
'this is a Post office box 25050 ',
'PO Box 25050 ',
'Post Box 25050 ',
);
for my $sample (@tests) {
if ($sample =~ /$regex/) {
print "Passed - $sample\n";
}
}
__END__
Passed - Post Box 25050https://stackoverflow.com/questions/4564004
复制相似问题