gpt4 book ai didi

java - 正则表达式:如何包含 "if"条件字符

转载 作者:行者123 更新时间:2023-11-30 08:58:55 25 4
gpt4 key购买 nike

我正在 java 中使用正则表达式制作日期提取器。问题是日期是 20-05-2014 而我的程序正在提取 0-5-14。简而言之,我怎样才能得到我正在检查日期第二个字符的字符?

    int count = 0;
String data = "HellowRoldsThisis20-05-2014. farhan_rock@gmail.comHellowRoldsThisis.farhan@gmail.com";
String regexOfDate = "((?<=[0])[1-9]{2})|((?<=[12])[0-9])|((?<=[3])[01])\\.\\-\\_((?<=[0])[1-9])|((?<=[1])[0-2])\\.\\-\\_((?<=[2])[0-9]{4})"; \\THE PROBLEM
String[] extractedDate = new String[1000];
Pattern patternDate = Pattern.compile(regexOfDate);
Matcher matcherDate = patternDate.matcher(data);

while(matcherDate.find()){
System.out.println("Date "+count+"Start: "+matcherDate.start());
System.out.println("Date "+count+"End : "+matcherDate.end());
extractedDate[count] = data.substring(matcherDate.start(), matcherDate.end());
System.out.println("Date Extracted: "+extractedDate[count]);
}

最佳答案

你可以试试正则表达式:

// (0[1-9]|[12][0-9]|[3][01])[._-](0[1-9]|1[0-2])[._-](2[0-9]{3})
"(0[1-9]|[12][0-9]|[3][01])[._-](0[1-9]|1[0-2])[._-](2[0-9]{3})"

Debuggex

关于java - 正则表达式:如何包含 "if"条件字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27459138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com