• 正则表达式匹配目标


    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!

  • 相关阅读:
    Python 常用内置函数
    Java Graphics 2D绘制图片 在Liunx上乱码
    LInux Centos7 重装yum
    Spring Boot 异步调用
    Linux 清除N天前的 日期文件夹(yyyy-MM-dd)
    Python 2.75升级3.6.3
    Linux 移除python Error: Trying to remove “yum”, which is protected
    Java Future 使用场景
    CF446D DZY Loves Games
    三 lambda表达式有什么用
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3642358.html
Copyright © 2020-2023  润新知