1

I'm messing with wp_rewrite and have spent the past few hours trying to figure regular expressions out again (i seem to have forgotten.) This is probably extremely easy for someone. Basically I'd like to match an alpha expression and stop at teh first occurance of a forward slash. Here's what i'm doing:

I have a url

http://www.mydomain.com/basename/uniquename/page/2/

I want to get uniquename out with the following

'basename/(.*)/page/([0-9]{4})/?$' => 'index.php?post_type=basename&uniquenametaxonomy='.$wp_rewrite->preg_index(1).'&paged='.$wp_rewrite->preg_index(2),

obviously (.*) matches /page/ as well as uniquename and provides me with a query string looking like

?post_type=basname&uniquenametaxonomy=uniquename%2Fpage%2F2

I'd like it to be

?post_type=basname&uniquenametaxonomy=uniquename&paged=2

I know it has to be extremely easy, I'd like to match uniquename and stop at the first occurance of a forward slash.

3 Answers 3

2

Replace (.*) with (.*?). The question mark makes it 'lazy'.

Also, replace ([0-9]{4}) with (\d+).

See http://www.regular-expressions.info/reference.html

1
  • worked perfectly @scribu. New it was simple :)
    – user2271
    Jan 7, 2011 at 6:43
0

Wanted to update the answer letting you know that with all the extra wp_rewrites I was doing I needed to do

([a-z]*?) instead of (.*?) when I was parsing the url for words and @scribu's answer (\d+) when parsing numbers. But don't forget that ?

0

"Everything until the first forward slash" is expressed by [^/]*. The [] indicate a character class (everything in here matches), the ^ inverts it (everything not in here matches), and the * repeats it.

If you are changing the rewrite rules I recommend my rewrite analyzer plugin (soon in the repository, but get the current version via Dropbox), it helps you debug these things.

Rewrite Analyzer screenshot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.