我有一个 json feed,它提供用于填充日历的 html,我需要从中检索一些信息。例如标题、时间和地点。我想使用正则表达式来获取
之间的内容<span class=\"title\">
和
<\/span><br/><b>
我正在尝试使用此代码
for(int i = 0; i < json.length(); i++)
{
JSONObject object = new JSONObject(json.getJSONObject(i));
System.out.println(object.getNames(object));
Pattern p = Pattern.compile("(?i)(<span class=\"title\">)(.+?)(<\\/span>)");
Matcher m = p.matcher(json.get(0).toString());
m.find();
System.out.println(m.group(0));
但它似乎没有完成这项工作...我已经尝试了多次迭代并尝试在线研究示例,但我不确定我是否在正则表达式语法中做错了什么。如有帮助,我们将不胜感激。
{"hoverContent":"<b>Title: <\/b><span class=\"title\">Accounting Awareness<\/span><br/><b>Time: <\/b><span class=\"time\">5:30 PM - 6:30 PM<br/><b>Location: <\/b><span class=\"location\">1185 Grainger Hall<\/span><br/><b>Description: <\/b><br/><span class=\"description\">Information from Kristen Fuhremann, Director of Professional Programs in Accounting and Q&A from a panel of current and former students who will share their experiences in the accounting program. Panel includes a grad of the IMAcc program currently in law school, a candidate for the IMAcc program who studied abroad, an accounting and finance double major, and an IMAcc student who is also a TA for AIS 100. Casual Attire is appropriate.<br />Contact: Natalie Dickson, <a href=\"mailto:ndickson@wisc.edu\">ndickson@wisc.edu<\/a><\/span><br/>","title":"Accounting Awareness","start":"2013-09-30 17:30:00","allDay":false,"itemId":"2356754a-8178-4afd-b4cf-7f5f5ce89868","end":"2013-09-30 18:30:00"}
空
最佳答案
m.group(0)
始终返回与正则表达式匹配的整个字符串。看起来您想返回特定组,因此您需要使用 m.group(1)
要获取与第一组匹配的文本,m.group(2)
对于第二组,依此类推。在此正则表达式中:
"(?i)(<span class=\"title\">)(.+?)(<\\/span>)"
括号中的任何内容,以 (?
开头的内容除外,算作一个组,因此 (.+?)
中的部分是第二个捕获组,您可以尝试使用 m.group(2)
检索它。在这种情况下,无需输入 <span
括号里的东西,所以你可以说
"(?i)<span class=\"title\">(.+?)<\\/span>"
现在使用m.group(1)
获取第一个(也是唯一的)捕获组。
关于java - 在java中使用正则表达式提取html语法中两个单词之间的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19641015/