java - Regex trying to get some text after text doesn't work -
i got code should retrieve id value 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
'true' 'atcherid.matches()'
but instead of i'm getting 'false' that:
exception in thread "main" java.lang.illegalstateexception: no match found @ java.util.regex.matcher.group(unknown source) @ mainclass.main(mainclass.java:19)
do miss here ?
if objective find id url, suggest simpler regex instead of using such long one.
sample:
string xmltag = "http://www.facebook.com/profile.asp?id=123456789"; string regexid = "\\?id=(\\d+)"; final pattern patternid = pattern.compile(regexid); final matcher matcherid = patternid.matcher(xmltag); system.out.println("found id = " + matcherid.find()); system.out.println("id = " + matcherid.group(1));
Comments
Post a Comment