[solved] prob with simple regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
kayak2013
Forum Newbie
Posts: 6
Joined: Fri Mar 15, 2013 1:46 am

[solved] prob with simple regex

Post by kayak2013 »

Hi

I'm trying write an htaccess rewrite condition to use

Code: Select all

^\/[a-z]{1}\/[\d]+\/$
to match with

Code: Select all

http://www.domain.com/p/568/foo/bar/pagename
but not with

Code: Select all

http://www.domain.com/ppp/568/foo/bar/pagename
so basically the string i need to match is "/p/568/" - just 1 lower-case letter between the first pair of slashes and any number of integers between the second pair

but ..... it's not working (obviously!) - could you help me with this ?

thanks
Last edited by kayak2013 on Sat Mar 16, 2013 2:51 am, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: prob with simple regex

Post by requinix »

Of course you need more than just "^\/[a-z]{1}\/[\d]+\/$". What's the rest of your .htaccess?

And did you know you can't capture parts of the URL without using parentheses? Like to hold onto the letter for later you need "([a-z])", then refer to it with $N (like $1 if it's the first grouping).
kayak2013
Forum Newbie
Posts: 6
Joined: Fri Mar 15, 2013 1:46 am

Re: prob with simple regex

Post by kayak2013 »

hi - thanks for your reply

I can do the capturing of bits of the url - that's not a problem - but that bit of code is part of a rewrite condition :

Code: Select all

RewriteCond %{REQUEST_URI} ^\/[a-z]{1}\/[\d]+\/$
what I would like is just to test it against the url before using it in the htaccess file
kayak2013
Forum Newbie
Posts: 6
Joined: Fri Mar 15, 2013 1:46 am

Re: prob with simple regex

Post by kayak2013 »

ok - I've set up a test situation with just this in the htaccess file (the rewrite condition is removed) :

Code: Select all

SetEnv PHP_VER 5

Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10  

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^\/([a-z]{1})\/([/d]+)\/$ test\.php?t=$1&p_id=$2 [NC]
I've tried all sorts and it just wont work with the url http://www.domain.com/p/568/foo/bar/pagename :banghead:

I get the 404 not found error message - have you got any ideas ??
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: prob with simple regex

Post by requinix »

You did turn the \d you had into /d, that's a problem. But mostly your expression only allows for /p/568/ with nothing after it.

Couple other comments, mostly about readability (always a good thing to aim for when dealing with regular expressions):
- You don't need to escape slashes: that's only needed for escaping them as regex delimiters but mod_rewrite doesn't use any
- {1} is redundant
- \d and [\d] mean the same thing
- The replacement URL isn't a regex
- 9/10 times you'll want the [L] flag too

Code: Select all

RewriteRule ^/?([a-z])/(\d+)/ test.php?t=$1&p_id=$2 [L,NC]
I use ^/? because depending on various circumstances the URL being matched against may or may not start with a slash.
kayak2013
Forum Newbie
Posts: 6
Joined: Fri Mar 15, 2013 1:46 am

Re: prob with simple regex

Post by kayak2013 »

arg! it's amazing how many mistakes one can make in so few characters !

it works fine now and sends the captured variables and all - excellent

many thanks for your help :D

..... now, on to the next part !
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: prob with simple regex

Post by requinix »

kayak2013 wrote:arg! it's amazing how many mistakes one can make in so few characters !
Welcome to the world of regular expressions :D To be fair they weren't mistakes so much as they were things you might have overcomplicated a bit.
kayak2013
Forum Newbie
Posts: 6
Joined: Fri Mar 15, 2013 1:46 am

Re: [solved] prob with simple regex

Post by kayak2013 »

ok
thanks again :D
Post Reply