WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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


I'd like it to be


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

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

"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

Your Answer


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.