In WordPress rewriting, I often see examples like this:
$wp_rewrite->add_rewrite_tag('%issue_project%', '(.+?/)?', 'issue_project=');
It has a ?
mark at the end of the regular expression. Some don't. What is the difference?
In WordPress rewriting, I often see examples like this:
$wp_rewrite->add_rewrite_tag('%issue_project%', '(.+?/)?', 'issue_project=');
It has a ?
mark at the end of the regular expression. Some don't. What is the difference?
It means a lazy rather than a greedy regular expression. See here.
What do lazy and greedy mean in the context of regular expressions?
.+
means one or more characters -- any characters
.+/
means one or more characters ending with the very last /
found. So given abc/def/gh/
the .+
matches abc/def/gh
.
.+?/
means one or more characters matching the shortest sequence, not the longest. So given abc/def/gh/
the .+
matches abc
.
The (whatever)?
trailing ?
after the parenthetical expression makes that expression optional. (whatever)?
means the same thing as (whatever){0,1}
, that is, it accepts zero or one repetitions of (whatever)
.