简体   繁体   中英

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 expression ( ^(?!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 /? .

顺便说一句,我一直在使用它,它的工作原理很奇妙: http : //www.rubular.com/它主要用于红宝石,但是在为php等通用正则表达式工作时也能很好地工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM