繁体   English   中英

java正则表达式正则表达式

[英]java regular expressions regex

我在从网站提取数据时遇到问题。 我正在尝试获取公司名称并为其定价: SYGNITY8,40

<a class="link" href="http://www.money.pl/gielda/spolki-gpw/PLCMPLD00016.html">SYGNITY</a>

        </td>
        <td class="ac"><a href="javascript: OO('SGN','2015-10-01')"><img width="12" height="11" src="http://static1.money.pl/i/gielda/chart.gif" title="Pokaż wykres" alt="Pokaż wykres" /></a></td>
                        <td class="al">SGN</td>
                    <td class="ar">8,40</td> 

我尝试使用这种模式,但它不起作用:

String expr = "<a class=\"link\" href=\"(.+?)\">(.+?)</a>(.+?)<td class=\"ar\">(.+?)</td> ";

有什么建议吗?

使用 JSoup 解析器

您应该使用像 JSoup 这样的 html 解析器,因为正则表达式不是解析 html 的好主意。

你可以这样做:

String htmlString = "YOUR HTML HERE";
Document document=Jsoup.parse(htmlString);
Element element=document.select("a[href=http://www.money.pl/gielda/spolki-gpw/PLCMPLD00016.html]").first();
System.out.println(element.text()); //SYGNITY

element=document.select("td[class=ar]").first();
System.out.println(element.text()); //8,40

使用正则表达式

如果您仍然想使用正则表达式,那么您可以使用如下所示的正则表达式并从捕获组中获取内容:

PLCMPLD00016.html">(.*?)<\/a>|"ar">(.*?)<\/td> 

工作演示

String htmlString = "YOUR HTML HERE"
Pattern pattern = Pattern.compile("PLCMPLD00016.html">(.*?)<\\/a>|"ar">(.*?)<\\/td>");

Matcher matcher = pattern.matcher(htmlString );
while (matcher.find()) {
    System.out.println(matcher.group(1));
    System.out.println(matcher.group(2));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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