Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

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

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

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

share|improve this answer
    
worked perfectly @scribu. New it was simple :) –  user2271 Jan 7 '11 at 6:43
add comment

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 ?

share|improve this answer
add comment

"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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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