我写了一个如下的正则表达式,它使用从字符串中提取日期:
(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 正则表达式之前,我在这里测试了http://regexr.com?35vlm
结果看起来没问题,它符合我想要的。
"el" 对象是一个字符串类型的数组列表:
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));
}
}
}
外部罐子: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 类。要解决这个问题,请将每个 \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}");
解决了这个问题。