-1

Does anyone have an idea how I can split a string returned by a WS to different strings?

String wsResult = "SONACOM RC, RUE DES ETOILES N. 20, 75250 PARIS (MI)";

I'm trying to split it into:

String name = "SONACOM RC";
String adress = "RUE DES ETOILES N. 20";
String postalCode = "75250";
String city = "PARIS";

N.B: the return of the WS changes only what is inside of my parameters

Thank you in advance for your help

ctwheels
  • 21,901
  • 9
  • 42
  • 77
KhaledZero
  • 42
  • 9
  • 2
    What have you tried so far? What didn't work? – BackSlash Jan 15 '18 at 15:43
  • You need a constant structure in order to use regexes or `.split`. It's not clear from your question if this condition does apply – GalAbra Jan 15 '18 at 15:44
  • Are you sure that there won't be any comma in the address part itself ? If it is the case you can use a regex to find the group. Something like this might work in your case: (.*),(.*), *(\d{5})(.*) You can check the behavior of regexp online, here for instance: https://www.regexplanet.com/advanced/java/index.html – Guillaume Jan 15 '18 at 15:47
  • Depends how you want to parse that information... `(?[\w .-]+?)\s*,\s*(?
    [\w .-]+?)\s*,\s*(?\d+)\s*(?[^()]+?)(?:\s+.*)?$` or `\s*,\s*|(?<=\b\d{5})\s+`
    – ctwheels Jan 15 '18 at 15:48
  • @ctwheels looks like Python regex, isn't it ? – Guillaume Jan 15 '18 at 15:50
  • @Guillaume the regex I posted will work in Java, Python, PCRE, among others. – ctwheels Jan 15 '18 at 15:51
  • I was totally unaware of named group in Java since jdk7! Thanks :) – Guillaume Jan 15 '18 at 16:23
  • I propose you the following regex `([^,]+),([^,]+),\s*(\d+)\s+([^(]+)(?= +\()`. – Andrei Odegov Jan 15 '18 at 18:04
  • Split on comma (if comma's aren't relative) then examine what's left after split. And, who knows the variants of whatever this string represents .. –  Jan 15 '18 at 21:07

2 Answers2

1

You could capture your data in 4 capturing groups. Your provided example uses uppercase characters, which you can match with [A-Z]. If you want to match also lowercase characters, digits and an underscore, you could replace [A-Z] or [A-Z\d] with \w. You can go about this in multiple ways. An approach could be:

([A-Z ]+), +([A-Z\d .]+), +(\d+) +([A-Z\d() ]+)

Explanation

  • Group 1: match one or more uppercase characters or a whitespace ([A-Z ]+)
  • Match a comma and one or more whitespaces , +
  • Group 2: match one or more uppercase characters or digit or whitespace or dot ([A-Z\d .]+)
  • Match a comma and one or more whitespaces , +
  • Group 3: match one or more digits (\d+)
  • Match one or more whitespaces +
  • Group 4: match one or more uppercase characters or digit or open/close parenthesis or whitespace ([A-Z\d() ]+)

Output in Java

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

One easy way to split it as you'd like is using wsResult.split(","). However, you'll have to add a comma between 75250 and Paris:

String wsResult = "SONACOM RC, RUE DES ETOILES N. 20, 75250, PARIS (MI)";
String[] temp = wsResult.split(",");
String name = temp[0];
String adress = temp[1];
String postalCode = temp[2];
String city = temp[3];

Using that you will get the output you're looking for.

EDIT

Another way to get your output without adding a comma would be to do this (using the code above too):

for(int i = 1; i<postalCode.length(); i++){

    if(postalCode.charAt(i) == ' ') {
        city = postalCode.substring(i,postalCode.length());
        postalCode = postalCode.substring(0,i);
        break;
    }

}

For more information check the String class in the API Java and this Stack Overflow question.

Alex Cuadrón
  • 638
  • 12
  • 19