开发者

How do I extract with non greedy across multiple lines in java regular expressions?

开发者 https://www.devze.com 2023-03-26 14:41 出处:网络
If I have a bunch of data across multiple lines, how do I make it non greedy?What I have is greedy. example data

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 &#45; load average&#58; 0&#46;01&#44; 0&#46;04&#44; 0&#46;05&nbsp;</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).

0

精彩评论

暂无评论...
验证码 换一张
取 消