1、html
<div class="weather"> <ul class="ui_top"> <li class="day">白天</li> <li class="icon"><div class="spritesweather" id="d01"></div></li> <li class="temp font_high">高温24℃</li> <li class="weather_desc">多云</li> <li class="wind">微风</li> </ul> <ul class="ui_bottom"> <li class="day">夜晚</li> <li class="icon"><div class="spritesweather" id="n00"></div></li> <li class="temp low_temp">低温12℃</li> <li class="weather_desc">晴</li> <li class="wind">微风</li> </ul> </div>
.java
// .匹配除换行符以外的任意字符 // *重复零次或更多次 // ?:限定符,表示0次或一次 // s表示空格,\s是加上转义字符的结果 // String regex = "<td.*?><p.*?>(.*?);.*?</td>"; String regex = "<li.*?\s*>(.*?\s*.*?)</li>"; Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(subhtml); while (m.find()) {// m.group(1)是当前搜到的符合要求的字符串,m.group(0)是搜的所有字符串 String group1 = m.group(1); if (group1.contains("<")) continue; System.out.println(group1); }
结果
白天
高温24℃
多云
微风
夜晚
低温12℃
晴
微风
2、去掉所有的空格,空行,制表符等
// 替换所有空白字符,包括换行,空格,tab html = html.replaceAll("\s*", "");
3、去掉除空格之外的空白地方
html = html.replace("
", "");
html = html.replace("
", "");
Done!