简体   繁体   English

基于工作的PHP代码在Java中使用Regex

[英]Using Regex in Java based on working php code

I've been using this in php... 我一直在用php ...

preg_match_all('|<a href="http://www.example.com/photoid/(.*?)"><img src="(.*?)" alt="(.*?)" /></a>|is',$xx, $matches, PREG_SET_ORDER);

where $xx is the entire webpage content as a string, to find all occurrences of matches. 其中$ xx是整个网页内容的字符串,用于查找所有匹配项。

This sets $matches to a two dimensional array which I can then loop through with a for statement based on the length of $matches and use for example .. 这将$ matches设置为二维数组,然后我可以根据$ matches的长度使用for语句循环并使用例如..

$matches[$i][1] which is would be the first (.*?) $matches[$i][1]这将是第一个(.*?)

$matches[$i][2] which is would be the second (.*?) $matches[$i][2]这将是第二个(.*?)

and so on.... 等等....

My question is how can this be replicated in java? 我的问题是如何在java中复制? I've been reading tutorials and blogs on java regex and have been using Pattern and Matcher but can't seem to figure it out. 我一直在阅读有关java正则表达式的教程和博客,并且一直在使用Pattern和Matcher,但似乎无法弄明白。 Also, matcher never finds anything. 此外,匹配器永远找不到任何东西。 so my while(matcher.find()) have been futile and usually throws an error saying no matches have been found yet 所以我的while(matcher.find())是徒劳的,通常会抛出一个错误,说没有找到匹配

This is my java code for the pattern to be matched is ... 这是我要匹配的模式的java代码是......

String pattern = new String(
    "<a href=\"http://www.example.com/photoid/(w+)\"><img src=\"(w+)\" alt=\"(w+)\" /></a>");

I've also tried .. 我也试过..

String pattern = new String(
    "<a href=\"http://www.example.com/photoid/(.*?)\"><img src=\"(.*?)\" alt=\"(.*?)\" /></a>");

and

String pattern = new String(
    "<a href=\"http://www.example.com/photoid/(\\w+)\"><img src=\"(\\w+)\" alt=\"(\\w+)\" /></a>");

no matches are ever found. 找不到匹配项。

Not an expert on Java, but shouldn't the strings escape double quotes and escapes? 不是Java专家,但字符串不应该转义双引号和转义?

 "<a href=\"http://www.mysite.com/photoid/(.*?)\"><img src=\"(.*?)\" alt=\"(.*?)\" /></a>"
 or
 "<a\\ href=\"http://www.mysite.com/photoid/(.*?)\"><img\\ src=\"(.*?)\"\\ alt=\"(.*?)\"\\ /></a>"

The regex you posted worked for me so perhaps your fault is in how you use it : 你发布的正则表达式对我有用,所以也许你的错是你如何使用它:

String test = "<html>\n<a href=\"http://www.mysite.com/photoid/potato.html\"><img src=\"quack-quack\" alt=\"hi\" /></a>\n</html>";
// This is exactly the pattern code you 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 this should be used. 请参阅Java Tutorial ,了解如何使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM