zoukankan      html  css  js  c++  java
  • 正则表达式匹配目标

    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!

  • 相关阅读:
    3、文件、函数练习题
    迭代器
    MySQL必知必会-用正则表达式进行搜索
    MySQL必知必会-检索数据
    数据结构与算法分析-树
    数据结构与算法分析-队列
    数据结构与算法分析-栈
    数据结构与算法分析-表
    数据结构与算法分析-算法分析
    数据结构与算法分析-引论
  • 原文地址:https://www.cnblogs.com/xingyyy/p/3642358.html
Copyright © 2011-2022 走看看