【问题标题】:Regex to extracting links from HTML [duplicate]正则表达式从 HTML 中提取链接 [重复]
【发布时间】:2010-08-10 19:36:31
【问题描述】:

可能重复:
Extract text and links from HTML using Regular Expressions

给定一个包含 HTML 的字符串,例如:

       <td scope="row">1</td> 
        <td scope="row"></td> 
        <td scope="row"><a href="/Archives/directory.htm">directoryfile.htm</a></td> 
        <td scope="row">directoryfile</td> 
        <td scope="row">104569</td> 
     </tr> 
     <tr class="blueRow"> 
        <td scope="row">2</td> 
        <td scope="row"></td> 
        <td scope="row"><a href="/Archives/historicaldata.htm</a></td> 
        <td scope="row">historicaldata</td> 
        <td scope="row">40361</td> 
     </tr> 
     <tr> 
        <td scope="row">&nbsp;</td> 
        <td scope="row"><span class="blue">Complete submission text file</span></td> 
        <td scope="row"><a href="/Archives/businessagenda.txt</a></td> 
        <td scope="row">businessagenda;</td> 
        <td scope="row">146701</td> 

我只想使用正则表达式获取历史数据的链接。但是,我的程序似乎没有找到链接,并且我没有看到问题,因为正则表达式适用于测试仪。大家看看有什么问题吗?

我知道正则表达式不是与 HTML 一起使用的最佳选择,但我只是想尝试一下。谢谢大家!

模式数据 = Pattern.compile("Archives.*\s.*historicaldata");
匹配器 test1= data.matcher(inputHTML);

而 (test1.find()) {
System.out.println("测试:现在匹配"); // 不打印
}

【问题讨论】:

  • 鸭子,在“ARRGGH!用正则表达式解析 Html!”之前人群向你扔鞋。
  • 我会看到您的“使用正则表达式解析 html”,并提出“布局表”、“以颜色命名的 css 类”和“文件名中的 .htm 而不是 .html”

标签: regex


【解决方案1】:

如果你只是想匹配 'Archives\historicaldata' 那么你的正则表达式字符串应该是 "Archives\/historicaldata"其实你可以用"Archives/historicaldata"

【讨论】:

    【解决方案2】:

    在你的模式"Archives.*\s.*historicaldata"

    Pattern data = Pattern.compile("Archives.*\s.*historicaldata");
    

    \s 表示空格[1],由于"/Archives/directory.htm" 中没有空格,因此不匹配。试试吧

    Pattern data = Pattern.compile("Archives.*historicaldata");
    

    [1] "\s" 也不正确 - 要在模式中得到它,您必须转义反斜杠,使其变为 "Archives.*\s.*historicaldata"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-04
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 2010-12-31
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      相关资源
      最近更新 更多