开发者

Negating Regex PO BOX

I have a below regular expression which returns true if it finds PO Box office combination

\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b

I want the exact opposite of this if a particular string has a combination of po 开发者_运维问答box office then it should return false else allow every thing

can someone help me with this please


try mine:

// 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]));


Pseudo code:

if not regex.matches(string)
  ...
end if

There is no easy way to make a regex match "everything but a complex expression".

Match the expression and negate the result.

Also, your regex is way to complicated. Try

\bp(ost)?[.\s-]*o(ffice)?[.\s-]+box\b

with the single-line-mode and ignore-case flags set. I don't think there really is a need to match a 0 in place of o, but that's up to you. Use [o0] if you must.


Thanks a lot for your help guys but I found the solution

(?i:^(?!([\s|\0-9a-zA-Z. ,:/$&#'-]*|p[\s|\.|, ]*|post[\s|\.]*)(o[\s|\.|, ]*|office[\s|\. ]*)(box[\s|\. ]*))[0-9a-zA-Z. ,:/$&#'-]*$)


After stripping the |'s in the character classes, and removing some inappropriate escapes, I tried your regex in Perl. Seems OK, albeit a bit negative (?!).

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 25050
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜