简体   繁体   中英

WordPress Rewrite regex: what does the trailing ? mean?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM