Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 匹配西班牙语日期的正则表达式_Java_Regex - Fatal编程技术网

Java 匹配西班牙语日期的正则表达式

Java 匹配西班牙语日期的正则表达式,java,regex,Java,Regex,我正在尝试用正则表达式匹配西班牙语中的几种日期格式 例如: 03德梅奥2020 03年月日。2020年 03年月日。del 2020 03 DE Mayo 20 03年月日。20 03年月日。del 20 3德梅奥2020 五月三日。2020年 五月三日。del 2020 3 DE Mayo 20 五月三日。20 五月三日。del 20 什么样的正则表达式可以与此匹配?下面的正则表达式将与您列出的所有日期格式匹配,但是,您尚未描述希望正则表达式排除的内容。如果选择不够,请告诉我 ^\\d{1,

我正在尝试用正则表达式匹配西班牙语中的几种日期格式


例如:
03德梅奥2020
03年月日。2020年
03年月日。del 2020
03 DE Mayo 20
03年月日。20
03年月日。del 20
3德梅奥2020
五月三日。2020年 五月三日。del 2020
3 DE Mayo 20
五月三日。20 五月三日。del 20


什么样的正则表达式可以与此匹配?

下面的正则表达式将与您列出的所有日期格式匹配,但是,您尚未描述希望正则表达式排除的内容。如果选择不够,请告诉我

^\\d{1,2} DE (?:\\w{3}\\.|\\w+) (?:del )?(?:\\d{4}|\\d{2})$
说明:

  • ^
    将匹配锚定到字符串的开头
  • \\d{1,2}
    匹配一个或两个数字字符(今天)
  • DE
    字面上与“DE”匹配,这在所有格式中都存在
  • (?:\\w{3}\\.\\w+)
    要么匹配3个单词字符,然后匹配一个文字点,要么匹配一个或多个单词字符(这是月份)
  • 匹配文本空格(月份后始终有空格)
  • (?:del)?
    匹配“del”(如果存在),但不需要它
  • (?:\\d{4}|\\d{2})
    匹配2位或4位字符(这是年份)
  • $
    将匹配锚定到字符串的末尾
在Java中,要测试某个字符串
str
是否是这些日期格式之一,可以使用:

str.matches("^\\d{1,2} DE (?:\\w{3}\\.|\\w+) (?:del )?(?:\\d{4}|\\d{2})$");
ArrayList<String> matches = new ArrayList<>();
Matcher regex = Pattern.compile("\\d{1,2} DE (?:\\w{3}\\.|\\w+) (?:del )?(?:\\d{4}|\\d{2})").matcher(str);
while (regex.find()) {
    matches.add(regex.group());
}
或者,如果您想在
str
中列出这些日期格式的所有事件,您可以使用:

str.matches("^\\d{1,2} DE (?:\\w{3}\\.|\\w+) (?:del )?(?:\\d{4}|\\d{2})$");
ArrayList<String> matches = new ArrayList<>();
Matcher regex = Pattern.compile("\\d{1,2} DE (?:\\w{3}\\.|\\w+) (?:del )?(?:\\d{4}|\\d{2})").matcher(str);
while (regex.find()) {
    matches.add(regex.group());
}
ArrayList matches=new ArrayList();
Matcher regex=Pattern.compile(\\d{1,2}DE(?:\\w{3}\.\\w+)(:del)(?:\\d{4}\\\\d{2}).Matcher(str);
while(regex.find()){
matches.add(regex.group());
}