Performing a regular expression match in PHP using the preg suite, I understand that you can represent a conditional statement right within the regex.
I could hardly find any documentation online so I turned to Jeffrey E.F. Friedl's Mastering Regular Expressions.
The way I see it, something like /(?(?<=NUM:)\d+|\w+)/
should match a digit when it is preceded by NUM:
otherwise it should match a word.
But for some weird reason it always returns true and the match data doesn't make sense to me either. Can someone explain to me what's going on?
What I want to do is this:
preg_replace('/creat(?:e|ing)/i', 'make', $input)
but only when '/creat(?:e|ing)/i' is not surrounded by quotes.
in action, the input-output sequence I need is:
- input: create a white shirt.
output: make a white shirt.
- input: "create a white shirt."
output: "create a white shirt"
- input: hello create some good code.
output: hello make some good code.
- input: "hello" "make some" good "code."
output: "hello" "make some" good "code."
Thank you everybody!
Edit: I want to do something like: if there is an opening quote, make sure it has a closing pair before matching the keyword create
in this case. Hope that makes sense and is possible.