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 ?

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
paweb
  • 141
  • 1
  • 9

1 Answers1

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.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • `Matcher matcher = Pattern.compile("\\{\\%foreach\\s(\\w+)\\s:\\s(\\w+)\\%\\}\\s(.*?)\\s\\{\\%endforeach\\%\\}",Pattern.MULTILINE).matcher(parsedHtml);` works fine! thx – paweb May 08 '18 at 21:05
  • @paweb on an unrelated note, I think you intended to use `Pattern.DOTALL`, not `Pattern.MULTILINE` – Patrick Parker May 08 '18 at 21:15