I am using trying to a NginX substitution filter which allows the use of regular expressions. I can get it working in a basic manner, i.e. replacing phone with telephone, but I can't seem to get it to conditionally replace a string of text.
Here is a check of XML for examination:
eat apples cantaloupe bananas often
I would like to perform the following set of rules:
eat and ending with oftenbananas then replace bananas with and especially bananasI know that I can use something like this to make parts of the string available:
/(eat.*)(bananas)?(.*often)/
I could use the following rule to do the replacement assuming bananas is present:
$1 and especially $2 $3
But this will give an odd result if bananas is not present:
input
eat apples cantaloupe often
output:
eat apples cantaloupe and especially often
Do I need a look ahead operator? I've read this article but I'm still having trouble.
Try using a look ahead:
/(?=.*eat.*bananas.*often) bananas/
Alternative without lookahead:
Raw Match Pattern:
^eat(.*)(bananas)(.*)often$
Raw Replace Pattern:
eat \1 and especially \2 often
$sourcestring before replacement:
do not eat bananas often
eat apples cantaloupe often
eat apples cantaloupe bananas often
$sourcestring after replacement:
do not eat bananas often
eat apples cantaloupe often
eat apples cantaloupe and especially bananas often
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