简体   繁体   English

简单的正则表达式问题(PHP)

[英]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. 我正在尝试找出表达式左侧数组中的字符串( ^(?!home|members).* )。

Basically any url that is not: 基本上任何不是以下网址:

/home or /home/ or /members or /members/ should be true. /home/home//members/members/应该为true。 The problem I have is if the url is something like /home-asdf . 我的问题是网址是否类似于/home-asdf This counts as being in my list of excluded urls (which in my example only has 'home' and 'members'. 这被视为在我的排除URL列表中(在我的示例中仅包含“ home”和“ 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 ( $ ). 这种过滤掉开头的网址, homemembers只有在这些名字后面紧跟一个斜杠或问号( [/?] ),或字符串(结束$ )。

http://www.regular-expressions.info/ http://www.regular-expressions.info/

The dot . . operator matches all characters. 运算符匹配所有字符。 The * operator means the previous pattern will be repeated 0 or more times. *运算符表示先前的模式将重复0次或更多次。 That means the end of your route matches any character any number of times after the word home or members. 这意味着您的路线的末尾会在单词“ home”或“ members”之后多次匹配任何字符。 If you only want to match one or zero slashes, then change .* to /? 如果只想匹配一个或零个斜杠,则将.*更改为/? .

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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