• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Regex for PO Box Validation

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic