-2

I'm trying to extract some information from HTML that is surrounded by tags on the line above and the line below. When I run the regex pattern.compile the end result returns the string I want, but also the white space before and after, which I don't want.

Can anyone help me find a way to get rid of the line breaks and return an Array of just string seperated by ", "?

Here's my HTML:

<div class="name">
                        Jonna Lundell
                    </div>

Here's my Java code: Where result is the string of HTML text.

html = result;

    Pattern p = Pattern.compile("<div class=\"name\">(.+?)</div>", Pattern.DOTALL);
    Matcher m = p.matcher(html);

    while (m.find()) {
        nameList.add(m.group(1));}

Log.i("Names ", String.valueOf(nameList));

Which returns:

I/Names: [
                            Name 1
                        , 
                            Name 2
                        , 
                            Name 3
                        , 
                            Name 4
                        , 
                            Name 5
                        , 
                            Name 6
                        ]
1
  • m.group(1) => m.group(1).trim(). Commented Apr 27, 2020 at 9:52

1 Answer 1

0

Use the trim() method:

while (m.find()) {
    nameList.add(m.group(1).trim());
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.