gpt4 book ai didi

java - 使用正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 12:02:20 24 4
gpt4 key购买 nike

我在尝试使用我在 JavaScript 中使用的正则表达式时遇到问题。在网页上,您可能有:

<b>Renewal Date:</b> 03 May 2010</td>

我只想能够拉出 2010 年 5 月 3 日,记住网页不仅仅包含上述内容。我目前使用 JavaScript 执行此操作的方式是:

DateStr = /<b>Renewal Date:<\/b>(.+?)<\/td>/.exec(returnedHTMLPage);

我尝试按照有关 java.util.regex.Patternjava.util.regex.Matcher 的一些教程进行操作,但没有成功。我似乎无法将 (.+?) 翻译成他们能理解的东西??

谢谢,

诺尼尔

最佳答案

这是在 Java 中使用正则表达式的方式:

Pattern p = Pattern.compile("<b>Renewal Date:</b>(.+?)</td>");
Matcher m = p.matcher(returnedHTMLPage);

if (m.find()) // find the next match (and "generate the groups")
System.out.println(m.group(1)); // prints whatever the .+? expression matched.

Matcher 类中还有其他有用的方法,例如m.matches()。看看Matcher .

关于java - 使用正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2758708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com