Reputation: 301
I'm having some trouble with a regular expression in PHP. What I'm trying to achieve is to have placeholder tokens within a string which are then replaced with a value.
I'm using the following syntax.
Hello {{ username }}! Your order ID is {{ order }}.
Having little experience with regular expressions, I used Regex101 an online regular expression test environment and came up with the following expression.
({{(?:.*?)(?:property)(?:.*?)}})
Which worked fine, until I used more than one token on a single line and realised a huge flaw in the expression. I understand how the expression is evaluating and have labeled the image below with my understanding (correct me if I'm wrong).
I'd like to think I'm not far away from what I'm trying to achieve, but I'm at a loss so any help is appreciated.
There needs to be a property name within the token that is included in the pattern, in the image above the property is email
.
Thanks for any help!
Regex101: https://regex101.com/r/7Ezep5/1
Upvotes: 1
Views: 105
Reputation: 8332
The short answer:
.
matches anything (but new line). Change it to [^}]
meaning anything but }
. Like
({{(?:[^}]*?)(?:email)(?:[^}]*?)}})
I.e. if it doesn't find the string email
before a }
, it fails.
Upvotes: 1