Need regex to match rather similar URL patterns

Question!

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!



Answers

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)

By : m0skit0


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öäüÖÄÜ

By : stema


This video can help you solving your question :)
By: admin