(SOLVED) Using Regex in Java based on working php code -
i've been using in php...
preg_match_all('|<a href="http://www.mysite.com/photoid/(.*?)"><img src="(.*?)" alt="(.*?)" /></a>|is',$xx, $matches, preg_set_order);
where $xx entire webpage content string, find occurrences of matches.
this sets $matches 2 dimensional array can loop through statement based on length of $matches , use example ..
$matches[$i][1]
first (.*?)
$matches[$i][2]
second (.*?)
and on....
my question how can replicated in java? i've been reading tutorials , blogs on java regex , have been using pattern , matcher can't seem figure out. also, matcher never finds anything. while(matcher.find())
have been futile , throws error saying no matches have been found yet
this java code pattern matched ...
string pattern = new string( "<a href=\"http://www.mysite.com/photoid/(w+)\"><img src=\"(w+)\" alt=\"(w+)\" /></a>");
i've tried ..
string pattern = new string( "<a href=\"http://www.mysite.com/photoid/(.*?)\"><img src=\"(.*?)\" alt=\"(.*?)\" /></a>");
and
string pattern = new string( "<a href=\"http://www.mysite.com/photoid/(\\w+)\"><img src=\"(\\w+)\" alt=\"(\\w+)\" /></a>");
no matches ever found.
any appreciated.
resolved
i doing wrong things inside while(matcher.find()) loop.
this question can closed.
the regex posted worked me perhaps fault in how use :
string test = "<html>\n<a href=\"http://www.mysite.com/photoid/potato.html\"><img src=\"quack-quack\" alt=\"hi\" /></a>\n</html>"; // pattern code posted : string pattern = new string( "<a href=\"http://www.mysite.com/photoid/(.*?)\"><img src=\"(.*?)\" alt=\"(.*?)\" /></a>"); pattern p = pattern.compile(pattern); matcher m = p.matcher(test); m.find(); // returns true
see java tutorial on how should used.
Comments
Post a Comment