Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression alternation in conditional statement

Is there anyway in php to use alternation in a condition as part of a conditional statement? For example:

Imagine I had two backreferences "1" and "2" and needed to check whether either of them existed in order to carry out one of two patterns, like so

(?(1|2)foo|bar)

I'm using php 5.2.14 so don't have the benefit of escaping backreferences. Everytime I try anything remotely like this I get a compile error...

Is there any possible work-around or is it a case of doing a conditional within a conditional?

Regards

Chris

like image 336
user1033937 Avatar asked Dec 20 '25 07:12

user1033937


1 Answers

There is no syntax for this I know of. All you can do is:

(?(1)foo|(?(2)foo|bar))
like image 87
NikiC Avatar answered Dec 22 '25 22:12

NikiC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!