我写了一个正则表达式,如下所示,它使用从字符串中提取日期:
(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)(\*){0,2}\s+\d{1,2}\s+(January|February|March|April|May|June|July|August|September|October|November|December)\s+\d{4}
在转换为java regex表达式之前,我在这里测试了http://regexr.com?35vlm
结果看起来没问题,它符合我的要求。
“el”对象是字符串类型arraylist :
holiday: New Year's Day Wednesday 1 January 2014
holiday: Chinese New Year Friday 31 January 2014 Saturday 1 February 2014
holiday: Good Friday Friday 18 April 2014
holiday: Labour Day Thursday 1 May 2014
holiday: Vesak Day Tuesday 13 May 2014
holiday: Hari Raya Puasa Monday 28 July 2014
holiday: National Day Saturday 9 August 2014
holiday: Hari Raya Haji Sunday* 5 October 2014
holiday: Deepavali Thursday** 23 October 2014
holiday: Christmas Day Thursday 25 December 2014
问题在java中有些日期被遗漏,有些是匹配的,我也在这里测试http://java-regex-tester.appspot.com/,同样的错误。
更新:
我的代码的完整版本:
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class Tester {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
updateSingaporeHolidayCalendar();
}
public static void updateSingaporeHolidayCalendar() throws IOException{
String url = "http://www.mom.gov.sg/employment-practices/leave-and-holidays/Pages/public-holidays-2014.aspx";
Document document = Jsoup.connect(url).get();
Elements holidays = document.select("#contentarea table tr");
// System.out.println("12312312");
//System.out.println("web page context: " + question);
List<String> el = new ArrayList<String>();
for(int i = 2; i < holidays.size() + 1; i++){
if((i&1) == 1) continue;
Elements threeGroup = holidays.get(i-2).getElementsByTag("td");
int j = 2;
for(Element e : threeGroup){
if(j-- != 0) continue;
j = 2;
el.add(e.text());
}
}
Pattern pattern = Pattern.compile("(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)(\\*){0,2}\\s+\\d{1,2}\\s+(January|February|March|April|May|June|July|August|September|October|November|December)\\s+\\d{4}");
//out put
for(int k = 0; k < el.size(); k++){
Matcher matcher = pattern.matcher(el.get(k));
// Check all occurrences
while (matcher.find()) {
//System.out.print("Start index: " + matcher.start());
//System.out.print(" End index: " + matcher.end());
System.out.println(" Found: " + matcher.group());
}
System.out.println("holiday: " + el.get(k));
}
}
}
外部Jar:JSoup.jar
输出
Found: Wednesday 1 January 2014
holiday: New Year's Day Wednesday 1 January 2014
Found: Saturday 1 February 2014
holiday: Chinese New Year Friday 31 January 2014 Saturday 1 February 2014
holiday: Good Friday Friday 18 April 2014
Found: Thursday 1 May 2014
holiday: Labour Day Thursday 1 May 2014
holiday: Vesak Day Tuesday 13 May 2014
holiday: Hari Raya Puasa Monday 28 July 2014
holiday: National Day Saturday 9 August 2014
Found: Sunday* 5 October 2014
holiday: Hari Raya Haji Sunday* 5 October 2014
holiday: Deepavali Thursday** 23 October 2014
Found: Thursday 25 December 2014
holiday: Christmas Day Thursday 25 December 2014
holiday:
holiday:
解决:
正如@Pshemo所说,“你从网站获得的数据也包含可以用HTML编写的无中断空间,显然它不属于\类。要解决这个问题,用[\]替换每个\ s \ u00A0]包含此字符(使用Unicode标识符编写)。“
所以将表达式更改为:
Pattern pattern = Pattern
.compile("(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)(\\*){0,2}[\\s\u00A0]+\\d{1,2}[\\s\u00A0]+(January|February|March|April|May|June|July|August|September|October|November|December)[\\s\u00A0]+\\d{4}");
解决了这个问题。
答案 0 :(得分:1)
您从网站获得的数据还包含no-break space
,可以用HTML  
编写,显然它不属于\\s
类。要解决此问题,请用\\s
替换每个[\\s\u00A0]
以包含此字符(使用Unicode标识符编写)。
所以你的正则表达式看起来像
Pattern pattern = Pattern
.compile("(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)(\\*){0,2}[\\s\u00A0]+\\d{1,2}[\\s\u00A0]+(January|February|March|April|May|June|July|August|September|October|November|December)[\\s\u00A0]+\\d{4}");
答案 1 :(得分:0)
嗯,我可以肯定地说你会错过最后两个,因为你的迭代会让2个元素缩短,即改变:
k < el.size() - 2
要
k < el.size()
或更好,使用foreach:
for (String s : el) {
Matcher matcher = pattern.matcher(a);
// ...
}
你的正则表达式看起来还不错。
答案 2 :(得分:0)
您的for循环for(int k = 0; k < el.size() - 2; k++)
仅限于el.size() - 2
尝试删除-2以循环遍历列表中的所有元素