Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   Help with Regular Expressions in Java (http://www.go4expert.com/forums/help-regular-expressions-java-t4101/)

sssrsl 1May2007 21:37

Help with Regular Expressions in Java
 
Hi

I need to check for a PO Box addresses in a string. I should not allow a pobox in a address Line. For that I need a pattern and willl match this pattern with my string

Ex:***** something like this ******
Pattern poBoxPattern = Pattern.compile("\\bp\\s*\\d") ....
Matcher matcher = poBoxPattern.matcher(locationAddressStreet);
if(matcher.find()){

}


So, could anyone help me with making the pattern.
My pattern should recognize sub strings like...
1. po 123 ( meaning: po followed by any integer/integers )
2. p o 123
3. p.o. 123
4. p. o. 123
5. po box
6. po box
7. p o box
8. p o 123

Note: "po sometext" or "p o sometext" should not be recognized.

Any help is truly appreciated.

Thanks
Sai

pradeep 2May2007 09:56

Re: Help with Regular Expressions in Java
 
Try this pattern

Code:

p[. ]?o([. ]+)?(\d+)

sssrsl 2May2007 20:42

Re: Help with Regular Expressions in Java
 
Perfect!! Thanks Pradeep!!

Customized it a little to suit my requirements.

Thanks again

Sai

sssrsl 2May2007 20:43

Re: Help with Regular Expressions in Java
 
Sorry :-( Forgot to add the pattern. Might be useful to others

"p[. ]?o([. ]+)?(\\d+)|\\bpo\\s*box\\b"

Thanks
Sai


All times are GMT +5.5. The time now is 13:56.