简体   繁体   English

Java-无法在此文本中使用正则表达式找到日期

[英]java - cannot find date using regex in this text

I have a text which contains also this kind of dates: ' Date Reported26/09/2010 08:22 ' and I want to extract the date with regex (because date is different in this taxes, so I am using this regex: 我有一个文本,其中也包含这种日期:' Date Reported26/09/2010 08:22 ',我想用正则表达式提取日期(因为该税款中的日期不同,所以我正在使用此正则表达式:

private static String PATTERN_DATE_REPORTED = "Date Reported[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}";

and do the extract like: 并像这样提取:

if ((value = extractWithRegEx(PATTERN_DATE_REPORTED, text)) != null) {
            DateFormat df = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
            try {
                metadata.setDateReported(df.parse(value));
                System.out.println("Succesfully converted report date: "+metadata.getDateReported().toString());
            } catch (ParseException e) {
                System.out.println("Exception occured when converting report date: "+ e.getMessage());
            }
        }else{
            System.out.println("Date Reported not found by this regex");
        }

where extractWithRegEx is defined like: 其中extractWithRegEx的定义如下:

public String extractWithRegEx(String regextype, String input) {
    String matchedString = null;

    if (regextype != null && input != null) {
        Matcher matcher = Pattern.compile(regextype).matcher(input);
        if (matcher.find()) {
            matchedString = matcher.group(0);
            if (matcher.groupCount() > 0) {
                matchedString = matcher.group(1);
            }
        }
    }
    return matchedString;
}

The issue: it does not find my date based on the above regex. 问题:它根据上述正则表达式找不到我的约会日期。

I am very sure the extractWithRegEx is correct because it works for other regex to find other type of text but for this date I think something is wrong in the rest of the code...Can you see the issue? 我非常确定 extractWithRegEx是正确的,因为它适用于其他正则表达式来查找其他类型的文本,但是就这一日期而言,我认为其余的代码有问题...您能看到问题吗?

您在正则表达式中缺少圆括号。

private static String PATTERN_DATE_REPORTED = "Date Reported([0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2})";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM