Denial of Regex PO BOX

I have the following regex that returns true if it finds an office combination PO Box

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

I need the exact opposite of this, if a particular line has a combination of po box office, then it should return false else, allowing every thing

can someone help me with this please

+2
regex
source share
4 answers

Pseudocode:

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

There is no easy way to make the regex "all but complex expression".

Match the expression and negate the result.

Also, your regular expression way is more complicated. Try

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

with the single-line mode and ignore checkboxes checked. I don't think you really need to match 0 instead of o , but that is up to you. Use [o0] if necessary.

+7
source share

try mine :

 // leon po box detection regex // for better results, trim and compress whitespace first var pobox_re = /^box[^az]|(p[-. ]?o.?[- ]?|post office )b(.|ox)/i, arr = [ "po box", "pob", "po box", "po-box", "po-box", "PO-Box", "po box", "pobox", "po-box", "po box", "post office box", "PO Box", "PO Box", "PO box", "box 122", "Box122", "Box-122", ]; for (var i in arr) console.log(pobox_re.test(arr[i])); 
+7
source share

Thank you very much for your help, but I found a solution

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

After removal | in character classes and removing some non-local screens, I tried your regular expression in Perl. It seems OK, although a little 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
source share

All Articles