File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Extracting a string with particular pattern from text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extracting a string with particular pattern from text" Watch "Extracting a string with particular pattern from text" New topic
Author

Extracting a string with particular pattern from text

Bart Stanz
Greenhorn

Joined: Feb 06, 2010
Posts: 13
Hello,

I was wondering if there is a 'proper' way in Java to extract a sub-string (from a string) that matches a particular pattern.

I want to extract a postal code from a full address string.

Postal code is A1A 1A1 format (where 1 is numeric between 0 and 9 and A is alphabetical A to Z).

The address example could be "123 Main Street, Toronto, Ontario, Canada, M3N 5H6" or "123 Main Street, Toronto, Ontario, Canada, 23456" (US zip code).
I would need to be able to extract M3N 5H6

Is there a way in Java to do some pattern matching in order to extract the postal code (if it exists) from the string? The assumption is that it could appear anywhere in the string.

Thanks!


www.bartium.com
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

You're looking for regex.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Note that you're asking two different things--do you need to be able to extract US postal codes as well?

Also note that unless data is assumed to be 100% formatted correctly there could be issues. And don't forget US ZIP+4.
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

Can you always guarantee the same format?

You could split the long string by each ",", take the last item in the produced array.

Then you can apply a regex to that, but that assumes that there is always going to be a postcode in the last section of the address String


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Bart Stanz
Greenhorn

Joined: Feb 06, 2010
Posts: 13
Thanks Wouter, just what I was looking for.

David, only Canadian postal codes, sorry I should've been more clear.

James, yes the postal code would be the same format, but it could appear anywhere in the string (meaning the address string is not always consistent format)

Thanks all,

-Bart
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extracting a string with particular pattern from text