Search code examples
phpregexregex-greedynon-greedy

Non greedy regex


I need to get the value inside some tags in a comment php file like this

php code
/* this is a comment
!-
<titulo>titulo3</titulo>
<funcion>
   <descripcion>esta es la descripcion de la funcion 6</descripcion>
</funcion>
<funcion>
   <descripcion>esta es la descripcion de la funcion 7</descripcion>
</funcion>
<otros>
   <descripcion>comentario de otros 2a hoja</descripcion>
</otros>
-!
*/
some php code

so as you can see the file has newlines and repetions of tags like <funcion></funcion> and i need to get every single one of the tags, so i was trying something like this:

preg_match_all("/(<funcion>)(.*)(<\/funcion>)/s",$file,$matches);

this example works with the newlines but its greedy so i've been searching and seen these two solutions:

preg_match_all("/(<funcion>)(.*?)(<\/funcion>)/s",$file,$matches);
preg_match_all("/(<funcion>)(.*)(<\/funcion>)/sU",$file,$matches);

but none of them work for me, don't know why


Solution

  • Try using [\s\S], which means all space and non-space characters, instead of .. Also, there's no need to add <funcion> and </funcion> in match groups.

    /<funcion>([\s\S]*?)<\/funcion>/s
    

    Also, keep in mind that the best way to do this is parsing the XML using a XML parser. Even if it's not a XML document, as you mentioned on your comment, extract the part that should be parsed and use XML parser to parse it.