Capture double quote content with recursion with Regex

Asked

Viewed 701 times

2

I need to capture double quotes with Regex in PHP. I tried using line-breaking recursiveness in several ways, but to no avail. I’ve combed through several topics and researched how to do that, but so far I haven’t been able to.

Block example:

mes "teste de mensagem";
input "blablabla";
switch("este é um":"placeholder");
end script;

Last code I used:

[.*\n]?"(.+?)"[.*\n]?(?1)*?

To test I’m using: https://regex101.com/

Remembering that there is no pattern for the rest, but what I need will always be inside a string with double quotes.

Thank you

  • In the call of preg_match_all() you used some PCRE modifier?

  • I’m trying with the multiline (m) modifier

  • But in this example it captures everything in quotes? what’s the problem?

  • It only captures the first one, I need it to capture all of them (including if you have more than one on the same line). That is, the recursion is not working...

  • 1

    Look at this example in case you’ll need to take everything in position 1 of the array, you can des/comment the example lines to see the difference.

  • It worked! He took all 317 strings, but in both positions 0 and 1 array. Why does it have two (and equal) positions? Edit: I just noticed, the difference is the quotes. Wonderful! Want to post as answer, so I mark as solution response?

  • 1

    I already put an answer, hahah the most difficult thing was to assemble the regex you did just switched function.

Show 2 more comments

1 answer

1


To catch all the captured occurrences use the function preg_match_all() and not preg_match(). The zero index has everything that was captured, while the others contain the result(capture) of the groups that are the expressions in parentheses.

Browser other questions tagged

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