Cristian Boariu
Cristian Boariu

Reputation: 9621

Why does this regex fail?

I have a PCL file and open it with Notepad ++ to view the source code (with PCL Viewer I see the final results but I need to view the source also).

enter image description here

Please see Lab Number and the rest of the characters. I am able to extract Lab Number and its code with this regex:

private static String PATTERN_LABNUMBER = "Lab Number[\\W\\D]*(\\d*)";

and it gives me:

0092616281

I now want to extract Date Reported and I use this regex (after a lot of other tries):

private static String PATTERN_DATE_REPORTED =
        "Date Reported[\\W\\D]*(\\d\\d/\\d\\d/\\d\\d\\d\\d \\d\\d:\\d\\d)";

but it does NOT find it in the PCL file.

I've also tried with:

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

but the same not found result... Do you see where I am missing something in this last regex?

Thanks a lot!

UPDATE:

I use this java code to extract Lab number and Date Reported:

 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()) {
                System.out.println("Matcher found for regextype "+regextype);
                matchedString = matcher.group(0);
                if (matcher.groupCount() > 0) {
                    matchedString = matcher.group(1);
                }
            }
        }
        return matchedString;
    }

Upvotes: 1

Views: 207

Answers (1)

Gursel Koca
Gursel Koca

Reputation: 21280

Here is the code to accomplish what you want..

       Pattern pattern =  Pattern.compile("Date Reported.*(\\d{2}/\\d{4} \\d{2}:\\d{2})$", Pattern.MULTILINE);
        String st = "date dfdsfsd fgfd gdfgfdgdf gdfgdfg gdfgdf 3232/22/2010 23:34\n"+
        "dsadsadasDate Reported gdfgfd gdfgfdgdf gdfgdfg gdfgdf 3232/22/2010 23:34";
        Matcher matcher = pattern.matcher(st);
        while (matcher.find()) {
            System.out.println(matcher.group(1));
        }

Upvotes: 2

Related Questions