php - How do I make a regex not 'swallow' a character? -
I have a text file like
{word} definition {second word} second Definition {word} They are not on different lines
I was using regex
$ regex = '/ \ {(az) + ) \} (+) \ {/ I '?.?
However, it causes problems in swallow the last brace, and then in the next word it will not match the next match.
To perform, I have done this for debugging purposes
echo preg_replace ($ regex, '& lt; b style = "background color: red;" & Gt; $ 1 & lt; / b & gt; & lt; b style = "background-color: yellow;" & gt; $ 2 & lt; / b & gt; '$ $ content;);
Here is an example of my output (In the next word the opening brace has no notice, so it does not match in regex)
& Lt ; B style = "background-color: red;" & Gt; Broom & lt; / B & gt; & Lt; B style = "background color: yellow;" & Gt;
How can I modify my regex to do this work
Multi-Stimed Woody Plant & Lt; / B & gt; Thank you
edit
Many thanks for your reply I have changed my regex so much
$ Regex = '/ \ {([az \ - \s] +?) \} ([^ \ {] +) / I';
I will also look into letterhead articles.
For this specific case, you can:
$ Regex = '/ \ {([az] +?)}} ([^ \ {] +) / I';
[^ \ {]
means "matches any character that is not a left brace". The advantage of this also is that {
] is not required at the end of your input.
More generally, you can be seen in others as mentioned.
Comments
Post a Comment