I have a regular expression
|^(/[a-z]{2})?/?(php)/([-a-z0-9]+)/?/?$|
Which match /php/anything.
Now I want a regular expression which match both
/php/anything
/java/anything
I tired |^(/[a-z]{2})?/?(php|java)/([-a-z0-9]+)/?/?$| but its not working.
Can someone help me with solution?
I want to keep all other part but change just (php) part.
Here is the code
preg_match("|^(/[a-z]{2})?/?(php|java)/([-a-z0-9]+)/?/?$|", 'php/anything')
But I get this warning and match doesn’t work
Warning: preg_match(): Unknown modifier 'j'
Thanks
You are using
|as the regex delimiter but you have a|inside the regex as alteration, which you need to escape. If you don’t the 2nd|is treated as the end of regex and the letter following itjis treated as regex modifier. Sincejis not a valid modifier, you get the error.You escape the
|as:Or you can use a different pair of delimiter: