Marc Sanders
Marc Sanders

Reputation: 392

Need regex to match rather similar URL patterns

I need a regex (to be used in .htaccess) with the following attributes, capturing up to a three digit number and the text following it:

Match:

/1-my-blog-post/  
/100-another-blog-post/

Do not match:

/1/  
/100/

So far I have:

RewriteRule ^(\d{1,2,3}\w+)/$ /post.php?s=$1 [L]

Thanks in advance because it's really bugging me!

Upvotes: 2

Views: 198

Answers (2)

stema
stema

Reputation: 92986

I see some problems in your regex

^(\d{1,2,3}\w+)/$

\d{1,2,3} has to be \d{1,3} you just give the min and the max amount

\w includes also numbers and _ but not -

Maybe a better solution would be

^/(\d{1,3}[a-zA-Z-]+)/$

See it here online on Regexr

[a-zA-Z-] is a character class that matches a character from what is defined inside. I don't know if the letter range a-zA-Z is fine for you, but you can add any character you want to match into the class like this [a-zA-ZöäüÖÄÜ&?-]

Upvotes: 1

m0skit0
m0skit0

Reputation: 25873

In Perl it would be

^\d{1,3}[^\d].*

I'm assuming the character after the 3 numbers cannot be number, of course, and it's from the start of the string recived (as you put it in your example)

Upvotes: 0

Related Questions