This should be simple but I'm a noob and I can't for the life of me figure it out. I'm trying to use regex to match text inside of special open/close tags: [p2][/p2]
So in this text:
apple [p2]banana[/p2] grape [p2]lemon[/p2]
it should match "banana" and "lemon". The regex I've worked up so far is:
(?<=\[p2\]).+(?=\[\/p2\])
But this is too greedy. It matches starting with the "b" in banana and ends with the "n" in lemon, matching banana[/p2] grape [p2]lemon. How do I just match banana and lemon?
This should do it:
(?<=\[p2\]).+?(?=\[\/p2\])
I added the question mark to make the quantifier non-greedy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With