Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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,大家好,谢谢到目前为止的帮助。我有一个正则表达式,用于在给定字符串中查找日期,但它似乎不起作用。有人能告诉我我做错了什么吗?代码是这样的 Pattern pattern = Pattern.compile("^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)$"); Matcher match = pattern.matcher(text1); List<String> removable1

大家好,谢谢到目前为止的帮助。我有一个正则表达式,用于在给定字符串中查找日期,但它似乎不起作用。有人能告诉我我做错了什么吗?代码是这样的

Pattern pattern = Pattern.compile("^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)$");
Matcher match = pattern.matcher(text1);

List<String> removable1 = new ArrayList<String>();             
while(match.find()) {
    removable1.add(match.group());
}
Pattern Pattern=Pattern.compile(“^(0[1-9]|[12][0-9]|[3[01])[-/.](0[1-9]| 1[012])[-/.](19 | 20)\\d\\d(?,)$”;
Matcher match=pattern.Matcher(text1);
List removable1=新建ArrayList();
while(match.find()){
removable1.add(match.group());
}

我知道字符串text1中包含一个日期“7/2/2013”,该日期未写入列表removable1。感谢您的帮助。

您的模式不允许使用个位数的天数和月份。将前导的
0
设置为可选:

^(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)$
(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\\1(?:19|20)?\\d\\d
另外,我不确定后面的逗号是做什么的。此外,您说“包含在字符串中”,但
^
$
将模式锚定到开始和结束。因此,如果要查找日期,您可能需要删除这些:

最后,如果要确保两个日期分隔符是相同的字符,可以执行以下操作:

(0?[1-9]|[12][0-9]|3[01])([- /.])(0?[1-9]|1[012])\\2(19|20)\\d\\d
最后,对于一些优化,避免捕获不需要的内容(并可能使世纪可选:

^(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)$
(?:0?[1-9]|[12][0-9]|3[01])([- /.])(?:0?[1-9]|1[012])\\1(?:19|20)?\\d\\d
试试这个

String text1="07/02/2013";
Pattern pattern = Pattern.compile("^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$");

您使用的正则表达式不正确。例如,任务部分匹配日期

0[1-9]|[12][0-9]|3[01])
这个正则表达式表示,如果日期为1~9,则应使用0作为前缀

要解决此问题,您应该添加?这意味着前面的项是可选的。因此,您可以将正则表达式字符串更改为

(0?[1-9]|[12][0-9]|3[01])[- /.](0?[1-9]|1[012])[- /.](19|20)\d\d
将来,您可以使用regular expression tester来调试这些问题。它们非常有用,有助于节省时间。例如,

您确定更改输入是有效的解决方案吗?^^我认为这是关于匹配给定的输入,而不是找到与模式匹配的输入。对于Java问题,OP应该使用。我不知道regex.uttool.com,但regexpal使用JavaScript的regex风格。这两个都使用JavaScript regex风格。嗨,一个简单示例e问题。假设日期被前后两个字符串包围,并且我想匹配这些特定字符串之间的正则表达式模式,这会起作用吗[^string1 string2(0?[1-9]|[12][0-9]| 3[01])([-/.])(0?[1-9]| 1[012])\\2(19 | 20)\\d\\d string3 string4$]@newtoprogramming是的(前提是这些方括号不是你的模式的一部分)。你为什么不试试呢?这正是我试图为之写一个正则表达式的行“在2013年7月2日评论(1.0版)”,我尝试了“在(:0?[1-9]|[12][0-9]|[3[01])([-/.])(?:0?[1-9]| 1[012])\\1(?:19 | 20)\\d\\d(1.0版)”和(?