2

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

Snehal
  • 329
  • 3
  • 14

4 Answers4

7

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.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • Exactly what I was going to say. This really is the best way. – NickAldwin Dec 30 '10 at 15:53
  • The problem is I am using ASP.NET validations and I want the above reg-ex to return false so that page.isvalid property is set to false – Snehal Dec 30 '10 at 16:00
  • @Snehal: So it's valid when the term "post office box" or one of its variants is *not* contained? I'm not sure if that is a useful approach. But here you go: `(?is:(?!^.*(complex expression here)))`. Note that this is a negative look-ahead expression, and the `(?is:)` in-line modifiers switch on case-insensitive and single-line mode for the regex (read http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx). – Tomalak Dec 30 '10 at 16:02
  • I tried this (?is:(?!^.*(\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b))) but its not working can you plz help me build the expression – Snehal Dec 30 '10 at 16:21
  • @Snehal: What does "it's not working" mean? Also, have you tried the simplified expression I suggested? – Tomalak Dec 30 '10 at 17:09
  • @Snehal: My bad, the expression two comments up is incorrect. Here's the one that works: `^(?is:(?!complex expression here).)*$` – Tomalak Dec 30 '10 at 18:00
  • @Snehal I hope you discovered case insensitive regex by now! – Simon_Weaver Dec 23 '13 at 21:05
  • I've changed the first `[.\s-]+` to `[.\s-]*` in your answer - so as to allow a match for `PO BOX` which is the USPS standard way to represent a PO Box – Simon_Weaver Dec 23 '13 at 21:28
7

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]));
jwheron
  • 2,553
  • 2
  • 30
  • 40
leeoniya
  • 71
  • 1
  • 1
  • copying and pasting this into a regex tester did not match any of the ones which start with just box – Matt Jul 09 '14 at 16:26
2

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. ,:/$&#'-]*$)
Snehal
  • 329
  • 3
  • 14
  • this didn't work either. Is this the real answer? Nothing has been marked as an answer. – Matt Jul 09 '14 at 16:31
0

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