paweb
paweb

Reputation: 141

Java Pattern Multiline

Hi i would like to find that code in HTML

{%foreach damagePhotos : photo%}
    <img src="{%=photo}" alt="" width="320" height="200"/>
{%endforeach%}

My regexp is:

Matcher matcher = Pattern.compile("\\{\\%foreach\\s(.*)\\s:\\s(.*)\\%\\}\\s(.*)\\s\\{\\%endforeach\\%\\}",Pattern.MULTILINE).matcher(parsedHtml);

And everything work fine untile i've got many of that pattern i html :(

for example:

<p>
    {%foreach carPhotos : photo%}
    <img src="{%=photo}" alt="" width="320" height="200"/>
    {%endforeach%}
</p>
<p>
    {%foreach damagePhotos : photo%}
    <img src="{%=photo}" alt="" width="320" height="200"/>
    {%endforeach%}
</p>

Then mather find one match and group(1) is:

carPhotos : photo%}    <img src="{%=photo}" alt="" width="320" height="200"/>    {%endforeach%}</p><p>    {%foreach damagePhotos

What is wrong with my regexp ?

Upvotes: 3

Views: 107

Answers (1)

Patrick Parker
Patrick Parker

Reputation: 4959

.* is greedy, meaning it will span across multiple foreach groups.

try adding a reluctant qualifier, i.e. .*?

Also, be aware of the limitations of using regex to parse HTML.

Upvotes: 3

Related Questions