Simple regex question (php)
I've been using this line in a routes file:
$route['^(?!home|members).*'] = "pages/view/$0";
The string in the array on the left of the 开发者_如何学Cexpression ( ^(?!home|members).*
) is what I'm trying to figure out.
Basically any url that is not:
/home
or /home/
or /members
or /members/
should be true. The problem I have is if the url is something like /home-asdf
. This counts as being in my list of excluded urls (which in my example only has 'home' and 'members'.
Ideas on how to fix this?
Try this modification:
^(?!(home|members)([/?]|$)).*
This filters out URLs beginning with home
or members
only if those names are immediately followed by a slash or question mark ([/?]
), or the end of the string ($
).
http://www.regular-expressions.info/
The dot .
operator matches all characters. The *
operator means the previous pattern will be repeated 0 or more times. That means the end of your route matches any character any number of times after the word home or members. If you only want to match one or zero slashes, then change .*
to /?
.
As an aside, I use this all the time and it works wonders: http://www.rubular.com/ Its predominantly for ruby but works well when working out general regex for php etc too.
精彩评论