Regex expression to find more than one occurrence in the same string

2

I'm developing a template engine in PHP for learning issues as I'm new to programming, and would like a way to find more than one #include () in my view, because the one I currently have does not find all the first.

    
asked by anonymous 08.02.2018 / 11:52

1 answer

1
  

[...] I would like a way to find more than one #include () in my view, since the one I currently have does not find all includes only the first one.

I suggest that you use global flag in your regex, because according to you it already finds the first include and does not continue to match.

Following is an example without the global flag and an example with the global flag

Example usage in your php program:

<?php
$subject = "string do seu arquivo que vai ser analisado aqui";
$pattern = '/(include .*)/'; //insira seu padrão regex aqui
preg_match_all($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

Note that instead of using preg_match is used preg_match_all , this causes all match instances of the pattern to be returned

    
08.02.2018 / 14:14