0

I have an HTML string (in German) like this:

<li>Peter Goldberg Dr. , Brünner Straße 19, A-1210, Tel +43-1-1234567 (N)</li>

It consists of 3 parts:

  • name of the person ("Peter Goldberg Dr.")
  • the address of the person ("Brünner Straße 19, A-1210")
  • and the tel no of the person or simply the rest of the string ("Tel +43-1-1234567 (N)")

I need to split the whole string into these 3 components without the HTML list tags <li> and </li>.

I am trying it with Pattern and Matcher classes, but I am doing something wrong for sure.

    Pattern myPattern = Pattern.compile("<li>.+,.+Tel.+</li>");
    Matcher mat = myPattern.matcher(eingabe[0]);

    while (mat.find()) {
        System.out.println(mat.group(0));
    }

Could someone please help?

Thanks a lot!!

1
  • 2
    The answers will tell you how to get those particular fields out of that particular string, but so what? Most regexes are meant to be used on other input strings, and just giving one example leaves a lot of questions unanswered. Will the address always have exactly one comma in the middle? Or can it have 0 or 2 or more? Does the telephone number always start with Tel? Can the person's name or telephone number have a comma in it? Depending on the answers to those questions, the posted answers may not be correct. Commented Aug 20, 2014 at 23:31

2 Answers 2

1

You can use this regex:

<li>(.*?), (.*), (.*)<\/li>

Working demo

enter image description here

MATCH 1
1.  [4-23]  `Peter Goldberg Dr. `
2.  [25-50] `Brünner Straße 19, A-1210`
3.  [52-78] `Tel +43-1-1234567 (N)`
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Rod_Algonquin and Fede!
There is something I still have to mention: In the second component (address) there can be any number of commas (,). I am not sure if this changes anything in your answers!
@user3704589 I know that's why I used a greedy operator. Using this the greedy operator will ignore all commans except the latest one which is the separator between address and telephone.
1

You can use this regex to get all the elements inside the li tag and split it:

String s = "<li>Peter Goldberg Dr. , Brünner Straße 19, A-1210, Tel +43-1-1234567 (N)</li>";
   Pattern myPattern = Pattern.compile("<li>(.*)</li>");
   Matcher mat = myPattern.matcher(s);
   String [] array;
   while (mat.find()) {
       array = mat.group(1).split(",");
       System.out.println("Name: " + array[0]);
       System.out.println("Address: " + array[1] + "," + array[2]);
       System.out.println("Telephone: " + array[3]);

   }

result:

Name: Peter Goldberg Dr. 
Address:  Brünner Straße 19, A-1210
Telephone: Tel +43-1-1234567 (N)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.