How do I extract with non greedy across multiple lines in java regular expressions?
If I have a bunch of data across multiple lines, how do I make it non greedy? What I have is greedy.
example data
</TD>
<TD CLASS='statusEven'><TABLE BORDER=0 WIDTH='100%' CELLSPACING=0 CELLPADDING=0><TR><TD ALIGN=LEFT><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0>
<TR>
<TD ALIGN=LEFT valign=center CLASS='statusEven'><A HREF='extinfo.cgi? type=2&host=localhost&service=Current+Load'>Current Load</A></TD></TR>
</TABLE&g开发者_运维问答t;
</TD>
<TD ALIGN=RIGHT CLASS='statusEven'>
<TABLE BORDER=0 cellspacing=0 cellpadding=0>
<TR>
</TR>
</TABLE>
</TD>
</TR></TABLE></TD>
<TD CLASS='statusOK'>OK</TD>
<TD CLASS='statusEven' nowrap>08-04-2011 22:07:00</TD>
<TD CLASS='statusEven' nowrap>28d 13h 18m 11s</TD>
<TD CLASS='statusEven'>1/1</TD>
<TD CLASS='statusEven' valign='center'>OK - load average: 0.01, 0.04, 0.05 </TD>
Here's my code so far
Pattern p = Pattern.compile("(?s)<TD ALIGN=LEFT valign=center CLASS(.*)?<TABLE");
Matcher m = p.matcher(this.resultHTML);
if(m.find())
{
return m.group(1);
}
Ungreedy:
Pattern.compile("(?s)<TD ALIGN=LEFT valign=center CLASS(.*?)?<TABLE");
Also, check this:
Java Regexp: UNGREEDY flag
I've implemented UNGREEDY
for JDK's regex.
To make a quantifier non-greedy, you add a question mark immediately after it:
.* // greedy
.*? // non-greedy
What you've got there - (.*)?
- is a greedy .*
in a capturing group, said group being optional (the ?
is serving in its original role, as a zero-or-one quantifier).
精彩评论