• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Regex for PO Box Validation  RSS feed

 
Prothom Alo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am using following regex for PO BOX pattern validation -


Here is some test result -

PO Box : Valid
P O Box : Valid P. O. Box : Valid P.O.Box : Valid Post Box : Valid Post Office Box : Valid Post Office : Invalid P.O.B : Valid P.O.B. : Valid POB : Valid Post Office Bin : Valid Box : Invalid Bin : Invalid Post : Invalid Postal Code : Invalid 100,, P O Box Des Moines : Valid P O Box DesMoines1000 : Valid P O Box Des Moines 1000 : Valid Post Office Box : Valid Post Office Box : Valid Post Box # : Valid

Hope Bulevard : invalid
Pablo Avenue : invalid

Only issue I am having where following checks are failing -
Campbell
P Browley
Camp Betty
Hepburn

Any help would be highly appreciated. Thank you.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
546
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This part says 0 or 1 dot followed by 0 whitespace. I think you want a dot or single whitespace character followed by 0 or more additional whitespace.
 
Prothom Alo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne, Can you please post the full regex for your possible solution? I will test it out. Thank you.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37496
546
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sz ms wrote:Jeanne, Can you please post the full regex for your possible solution? I will test it out. Thank you.

Give it a try. To avoid the long regexp, see if you can write a reg exp that matches these:
O. B
O B

but not these:
O.B
OB

Post the regexp you try for this. And if it doesn't work, which of the four test scenarios it fails.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamie Zawinski, in a Tue 12 Aug 1997 Usenet post wrote: Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
 
Prothom Alo
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also tried following regex -



This resolves the previous issues such as

Campbell
P Browley
Camp Betty
Hepburn

However, it causes another issue. Now the validation fails for -

P.O.B
P.O.B.
POB
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Where did you find that regex? What is the grammar of PO box numbers you are trying to match? Why do you think a regex is the best way to do it?

This question is too hard for “beginning” so I shall move it.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things that may help you:

You have [\\w\\s*\\W]* on each side of the regex which means you could use Matcher.find() instead of matches(). The downside is this isn't built into java.lang.String so you have to use Pattern and Matcher:



Second, you can break your regex into pieces to make it more readable. Here's the regex I was using broken up:


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!