1

I want to be able to parse a String into several Strings of information using regular expressions. Let's for instance use an address: "My Street 10 90210 Beverly Hills". An example of Strings I could create from this:

  • My Street – street name
  • 10 – house number
  • 90210 – zip code
  • Beverly Hills – city

I've been trying to create a pattern for it use, but I'm a little lost when it comes to analyze the String and dissect it.

Can someone get me started on this?

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Aphex
  • 408
  • 1
  • 5
  • 17
  • 3
    It works best if you show us what you've tried. On So we prefer not to just write the code on request. This is not a programming to order site. – Brian Tompsett - 汤莱恩 Jan 29 '15 at 16:05
  • My question was more intended for people to link me to a specific class/Docs that I could read and obtain the information I need to continue. – Aphex Jan 29 '15 at 16:06
  • is that the zip code is always a 5 digit number? – Avinash Raj Jan 29 '15 at 16:07
  • I'm from Denmark, so in my case zip codes are four digit numbers, so my AIM is to make it work with four digit codes – so to answer your question; no. – Aphex Jan 29 '15 at 16:08
  • Your question is as old as regex itself. Have a look at http://stackoverflow.com/a/20437712/860196 - it might be better not to use regex for addresses. – runDOSrun Jan 29 '15 at 16:11
  • Thanks for the tip, Brian. Will go look into that. – Aphex Jan 29 '15 at 16:11
  • Thanks for the link, runDOS. It's an assignment, so we don't really have a choice whether we want to use it or not. – Aphex Jan 29 '15 at 16:13
  • "*My question was more intended for people to link me to a specific class/Docs*" in that case your question is off-topic because "*Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam.*". Stack Overflow is for *specific* problems preferably with code you already have. – Pshemo Jan 29 '15 at 17:12

1 Answers1

3

I would use string.split function.

String s = "My Street 10 90210 Beverly Hills";
String parts[] = s.split("\\s+(?=\\d+\\s+\\d+)|(?<=\\d+)\\s+(?=[A-Z])|(?<=\\d+)\\s+(?=\\d+)");
System.out.println(Arrays.toString(parts));

Output:

[My Street, 10, 90210, Beverly Hills]

Explanation:

  • \\s+(?=\\d+\\s+\\d+) Matches one or more spaces only if it's followed by one or more digits plus one or more spaces plus one or more digits. So that space before house number would satisfy this condition . So it got matched.

  • | Called alternation operator.

  • (?<=\\d+)\\s+(?=[A-Z]) Matches one or more spaces which are preceded by one or more digits and then followed by a capital letter. So the spaces before the string city would satisfy this condition and got matched.

  • (?<=\\d+)\\s+(?=\\d+) This matches all the spaces which are in-between the digits. So the spaces between house-number and zip-code got matched.

  • Splitting your input according to the matched spaces will give you the desired output.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thanks! This solved my problem – could you put a few words on how you went on creating that regular expression? And where I could learn to create a similar expression? Again, thanks for your time! – Aphex Jan 29 '15 at 19:55
  • added some explanation. www.regular-expressions.info best site for learning regexes. – Avinash Raj Jan 30 '15 at 00:19