我在尝试使用我在 JavaScript 中使用的正则表达式时遇到问题。在网页上,您可能有:
<b>Renewal Date:</b> 03 May 2010</td>
我只想能够拉出 2010 年 5 月 3 日,记住网页不仅仅包含上述内容。我目前使用 JavaScript 执行此操作的方式是:
DateStr = /<b>Renewal Date:<\/b>(.+?)<\/td>/.exec(returnedHTMLPage);
我尝试按照有关 java.util.regex.Pattern
和 java.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/