0

我写了一个如下的正则表达式,它使用从字符串中提取日期:

(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}");

解决了这个问题。

4

3 回答 3

1

您从站点获得的数据也包含no-break space可以用 HTML 编写的数据&#160;,显然它不属于\\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}");
于 2013-08-16T04:53:47.317 回答
0

好吧,我可以肯定地说您会错过最后两个,因为您的迭代会停止 2 个元素,即更改:

k < el.size() - 2

k < el.size()

或者更好的是,使用 foreach:

for (String s : el) {
    Matcher matcher = pattern.matcher(a);
    // ...
}

你的正则表达式看起来不错。

于 2013-08-16T04:01:37.150 回答
0

您的 for 循环for(int k = 0; k < el.size() - 2; k++)仅限于el.size() - 2 尝试删除 -2 以遍历列表中的所有元素

于 2013-08-16T04:23:48.043 回答