Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extracting a string with particular pattern from text

 
Bart Stanz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're looking for regex.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bart Stanz
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic