简体   繁体   中英

Parsing data with Pattern in java

I get a response from server, which seems like:

<span class="name_cards">
<a href="LINK">NAME</a>     <img alt="" src="IMAGE" />
</span>
<table class="tbl_card">
<tr>
        <th colspan="2">
            BALANCE <span class="CURRENCY">TEXT</span>
        </th>
</tr>
</table>

<span class="name_cards">
<a href="LINK 2">TEXT 2</a></span>
<table class="tbl_card">
<tr>
        <th colspan="2">
            BALANCE 2 <span class="CURRENCY">TEXT</span>
        </th>
</tr>
</table>

How to create a regular expression to parse?
I tried

<span class="name_cards">((.|\n)*?)<\x2ftable>

on http://regexr.com/ and it worked, but when i put it in Android Studio, Matcher showed no matches.

In Java Strings a \\ needs to be escaped so try replacing \\x2 with \\x2

You will also have to escape the " to be \\" and the \\n (I should have read the regex more carefully the first time)

Basically you have to double all the \\ in your Strings in Java as the \\ tells the compiler do treat the next character as a control code.

Also android studio should tell you your regex is wrong, at least IntelliJ does then you do Pattern pattern = Pattern.compile("regex");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM