I need some regex help, please.
I have the following regex used in PHP:
$module_pattern = "/\[\[mod:(.*?):(.*?):(.*?):(.*?):(.*?)\]\]/i";
It should replace this regex with a module (mod:) and this specific module first :(.*?) and the specific function second :(.*?). I have tried adding the third, fourth and fifth :(.*?) in order to allow for passing parameters to the function, but the above regex assumes that we WILL in fact pass 3 parameters, but I might in some cases pass only one or two.
How do I fix this?
Thanks,
Kobus
You can create new, optional groups using
(...)?. The parameter would be(:(.*?))?. The reason I use a nested group is because you won’t want to capture the content, so we can add a?:to the outer group so it ignores it:(?::(.*?))?.Your pattern would look like:
/\[\[mod:(.*?):(.*?)(?::(.*?))?(?::(.*?))?(?::(.*?))?\]\]/i