Will this Java regular expression return a single result or several

If my HTML is:

<tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> <tr><td>....</td><hr></tr> 

If my regex is:

 Patterp p = Pattern.compile("<tr>(.*)<hr></tr>"); 

Should it get 1 result or all individual rows?

Is there a way to get it to get all the lines, not just the whole html from the top <tr> to the last instance <hr></tr> ?

+4
source share
1 answer

Your regex uses .* , Which is greedy. Use .*? Instead . A greedy match will grab as much as possible before matching the following tokens, so it will find the last <hr> in your source. An unwanted match will suffice as little as possible before matching the next token (s).

Then see this answer for more information on parsing HTML with regular expressions.

+11
source

Source: https://habr.com/ru/post/1307242/


All Articles