user3045798
user3045798

Reputation: 193

Regular Expression not matching data but Regular Expression confirmed working

I have the following Java program that grabs a date-time String and have tested in multiple regular expression test sites to check if they do indeed work, which they do.

private static final Pattern MATCH_DATE_TIME = Pattern.compile("<span id=\"lblMatchTime\" style=\"padding: 0px;\">([\\d+/ GMT:]+)</span>");

The unescaped version of the expression

<span id="lblMatchTime" style="padding: 0px;">([\d/ :GMT]+)</span>

and the websites I tested the above on.

With the following data

                    <div style="width: 30%; text-align: center; display: inline-block;">                             <span>                                 Time  :                             </span>                             <span id="lblMatchTime" style="padding: 0px;">05/28/2014 14:16:21 GMT</span>                         </div>                         <div style="width: 30%; text-align: center; display: inline-block;">                             <span>duration:                             </span>                             <span id="lblMatchDuration" style="padding: 0px;">50</span>                             <span>                                 minutes                             </span>                         </div>                     </div>

Both of the above websites with the unescaped versions correctly match the intended data below

<span id="lblMatchTime" style="padding: 0px;">05/28/2014 14:16:21 GMT</span>  //the matched data
05/28/2014 14:16:21 GMT //$1 extracted data

although both the above sites successfully match and extract the data my program does not.

Here is the code for my program (minimum code example)

private static final Pattern MATCH_DATE_TIME = Pattern.compile("<span id=\"lblMatchTime\" style=\"padding: 0px;\">([\\d+/ GMT:]+)</span>");

public static void main(String[] args)
{
    try
    {

        String data = JOptionPane.showInputDialog(null, "Please Enter The Match ID", "Grab Match Details", JOptionPane.PLAIN_MESSAGE);
        Matcher m = MATCH_DATE_TIME.matcher(data);
        System.out.println(m.matches());
        String dateTime = m.group(0);
        System.out.println(dateTime);
    }
    catch (IllegalStateException ise)
    {
        ise.printStackTrace();
    }
}

Upvotes: 0

Views: 119

Answers (3)

Bohemian
Bohemian

Reputation: 425238

I believe Sotirios is correct, however as an alternative, in cases like this I prefer a one-line solution not involving the clumsy Matcher api, but using replaceAll() instead:

String dateTime = input.replaceAll(".*(?:<span id=\"lblMatchTime\" style=\"padding: 0px;\">([\\d+/ GMT:]+)</span>)?.*", "$1");

This works by adding ".*" to either end of the pattern so the entire input is matched, then replacing it with the captured target using a back-reference $1 in the replacement to group 1. There's added regex special sauce of the entire expression wrapped in an optional non-capturing group to make the result blank if there's no match.

This avoids several lines of code, and bypasses tests for matching or not.

Upvotes: 0

user3045798
user3045798

Reputation: 193

private static final Pattern MATCH_DATE_TIME = Pattern.compile("<span id=\"lblMatchTime\" style=\"padding: 0px;\">([\\d+/ GMT:]+)</span>");

    public static void test()
    {
        try
        {

            String data = JOptionPane.showInputDialog(null, "Please Enter The Match ID", "Grab Match Details", JOptionPane.PLAIN_MESSAGE);
            Matcher m = MATCH_DATE_TIME.matcher(data);
            if(m.find())
            {
                System.out.println(m.group(1));
            }
        }
        catch (IllegalStateException ise)
        {
            ise.printStackTrace();
        }
    }

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280132

You'll want to use Matcher#find() instead of Matcher#matches(). find() searches the given string for a match while matches() attempts to match the entire string. Your regex does not match the entire string.

Upvotes: 5

Related Questions