3

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 ?

1 Answer 1

3

.* 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Matcher matcher = Pattern.compile("\\{\\%foreach\\s(\\w+)\\s:\\s(\\w+)\\%\\}\\s(.*?)\\s\\{\\%endforeach\\%\\}",Pattern.MULTILINE).matcher(parsedHtml); works fine! thx
@paweb on an unrelated note, I think you intended to use Pattern.DOTALL, not Pattern.MULTILINE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.