0

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?

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
yeahman
  • 2,737
  • 4
  • 21
  • 25

1 Answers1

2

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).

Community
  • 1
  • 1
O. Jones
  • 103,626
  • 17
  • 118
  • 172