[英]attributes pattern matcher takes a long time
I have a regex to get the src and the remaining attributes for all the images present in the content. 我有一个正则表达式来获取内容中存在的所有图像的src和其余属性。
<img *((.|\s)*?) *src *= *['"]([^'"]*)['"] *((.|\s)*?) */*>
If the content I am matching against is like 如果我要匹配的内容是
<img src=src1"/> <img src=src2"/>
the find(index) hangs and I see the following in the thread dump find(index)挂起,我在线程转储中看到以下内容
at java.util.regex.Pattern$LazyLoop.match(Pattern.java:4357)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4227)
at java.util.regex.Pattern$BranchConn.match(Pattern.java:4078)
at java.util.regex.Pattern$CharProperty.match(Pattern.java:3345)
at java.util.regex.Pattern$Branch.match(Pattern.java:4114)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4168)
at java.util.regex.Pattern$LazyLoop.match(Pattern.java:4357)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4227)
at java.util.regex.Pattern$BranchConn.match(Pattern.java:4078)
at java.util.regex.Pattern$CharProperty.match(Pattern.java:3345)
at java.util.regex.Pattern$Branch.match(Pattern.java:4114)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4168)
at java.util.regex.Pattern$LazyLoop.match(Pattern.java:4357)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4227)
at java.util.regex.Pattern$BranchConn.match(Pattern.java:4078)
at java.util.regex.Pattern$CharProperty.match(Pattern.java:3345)
at java.util.regex.Pattern$Branch.match(Pattern.java:4114)
at java.util.regex.Pattern$GroupHead.match(Pattern.java:4168)
at java.util.regex.Pattern$LazyLoop.match(Pattern.java:4357)
at java.util.regex.Pattern$GroupTail.match(Pattern.java:4227)
at java.util.regex.Pattern$BranchConn.match(Pattern.java:4078)
at java.util.regex.Pattern$CharProperty.match(Pattern.java:3345)
Is there a solution or a workaround for solving this issue? 是否有解决此问题的解决方案或解决方法?
A workaround is to use a HTML parser such as JSoup
, for example 一种解决方法是使用HTML解析器,例如JSoup
。
Document doc =
Jsoup.parse("<html><img src=\"src1\"/> <img src=\"src2\"/></html>");
Elements elements = doc.select("img[src]");
for (Element element: elements) {
System.out.println(element.attr("src"));
System.out.println(element.attr("alt"));
System.out.println(element.attr("height"));
System.out.println(element.attr("width"));
}
It looks like what you've got is an " evil regex ", which is not uncommon when you try to construct a complicated regex to match one thing (src) within another thing (img). 看起来您所获得的是一个“ 邪恶的正则表达式 ”,当您尝试构造一个复杂的正则表达式以将一个事物(src)与另一事物(img)匹配时,这种情况并不罕见。 In particular, evil regexs usually happen when you try to apply repetition to a complex subexpression, which you are doing with (.|\\s)*?
特别是,当您尝试对复杂的子表达式应用重复时,通常会出现邪恶的正则表达式,而您正在使用(.|\\s)*?
. 。
A better approach would be to use two regexes; 更好的方法是使用两个正则表达式。 one to match all <img>
tags, and then another to match the src
attribute within it. 一个匹配所有<img>
标签,然后另一个匹配其中的src
属性。
My Java's rusty, so I'll just give you the pseudocode solution: 我的Java很生锈,所以我只给您伪代码解决方案:
foreach( imgTag in input.match( /<img .*?>/ig ) ) {
src = imgTag.match( /\bsrc *= *(['\"])(.*?)\1/i );
// if you want to get other attributes, you can do that the same way:
alt = imgTag.match( /\balt *= *(['\"])(.*?)\1/i );
// even better, you can get all the attributes in one go:
attrs = imgTag.match( /\b(\w+) *= *(['\"])(.*?)\2/g );
// attrs is now an array where the first group is the attr name
// (alt, height, width, src, etc.) and the second group is the
// attr value
}
Note the use of a backreference to match the appropriate type of closing quote (ie, this will match src='abc'
and src="abc"
. Also note that the quantifiers are lazy here ( *?
instead of just *
); this is necessary to prevent too much from being consumed. 注意使用反向引用来匹配适当类型的结束引号(即,将匹配src='abc'
和src="abc"
。还请注意,这里的量词是懒惰的( *?
而不是*
);这是防止消耗过多的必需品。
EDIT: even though my Java's rusty, I was able to crank out an example. 编辑:即使我的Java生锈,我仍然能够提出一个例子。 Here's the solution in Java: 这是Java中的解决方案:
import java.util.regex.*;
public class Regex {
public static void main( String[] args ) {
String input = "<img alt=\"altText\" src=\"src\" height=\"50\" width=\"50\"/> <img alt='another image' src=\"foo.jpg\" />";
Pattern attrPat = Pattern.compile( "\\b(\\w+) *= *(['\"])(.*?)\\2" );
Matcher imgMatcher = Pattern.compile( "<img .*?>" ).matcher( input );
while( imgMatcher.find() ) {
String imgTag = imgMatcher.group();
System.out.println( imgTag );
Matcher attrMatcher = attrPat.matcher( imgTag );
while( attrMatcher.find() ) {
String attr = attrMatcher.group(1);
System.out.format( "\tattr: %s, value: %s\n", attrMatcher.group(1), attrMatcher.group(3) );
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.