Regex trying to get some text after text doesn’t work

I got this code which should retrieve the id value from the url.:

        String xmlTag = "http://www.facebook.com/profile.asp?id=123456789";
        xmlTag = xmlTag.replaceAll("/", "//");

        //regex variables
        final String regexUrl = "(?:(?:http|https):////)?(?:www.)?facebook.com//(?:(?:w)*#!//)?(?:[?w-]*//)?(?:profile.asp?id=(?=d.*))?([w-]*)?";
        final Pattern patternUrl = Pattern.compile(regexUrl);
        final Matcher matcherUrl = patternUrl.matcher(xmlTag);  

        String urlResult = matcherUrl.group(0);         
        System.out.println("group(0) = " + urlResult);
        String regexId = "(?<=http:////www.facebook.com//profile.asp?id=).*";
        System.out.println("regexId =   " + regexId);

        final Pattern patternId = Pattern.compile(regexId);
        final Matcher matcherId = patternId.matcher(urlResult);         
        System.out.println("id = " + matcherId.matches());

The output should be: 123456789 which is ‘true’ for ‘atcherId.matches()’

But instead of that i’m getting ‘false’ like that:

Exception in thread "main" java.lang.IllegalStateException: No match found
    at java.util.regex.Matcher.group(Unknown Source)
    at MainClass.main(MainClass.java:19)

Do I miss something here ?


Source: regex

Leave a Reply