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

Asked

Viewed 203 times

1

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

  • 2

    Put the code you are using to illustrate your problem. This way no one will be able to help you. Enjoy and take a [tour] through the platform to learn more ;)

1 answer

2


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

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

Follow an example without the global flag and an example with the global flag

Example of use 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 occurrences of Pattern to be returned

  • Thank you, it worked perfectly.

  • @Neversonsilva Uhu! just mark as correct now. This helps other people with the same question who enter this page to find the correct answer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.