[英]Perl Regex URL contains string but not similar string
I need to write regex for Perl so that it matches /contact
(and everything beyond so /contact/talk etc.). 我需要为Perl编写正则表达式,使其与
/contact
(以及/ contact / talk等之外的所有内容)匹配。
But I also have URL for /contacts
so I want it to skip this. 但是我也有
/contacts
URL,所以我希望它跳过这一点。
So far I've only been able to write code either for exact matches or for 到目前为止,我只能为完全匹配或针对
So something along the lines of: 因此,遵循以下原则:
if ($uri =~ m/contact/i ) {
## Redirect somewhere
}
This also redirects everything within /contacts
though which is where I'm struggling. 这也重定向了
/contacts
所有内容,尽管这是我一直在努力的地方。
if ($uri =~ m!/contact(?:/|$)!) {
#
}
if ($uri =~ m/\/contact(?:$|\/)/i ) {
## Redirect somewhere
}
Will match /contact
because of the $
, and everything like /contact/...
由于
$
,将匹配/contact
,以及/contact/...
类的所有内容/contact/...
\\b
is a word boundary marker, which prevents this match from being in the middle of a word: \\b
是单词边界标记,它可以防止此匹配出现在单词中间:
if ($uri =~ m|\bcontact\b|i ) {
## Redirect somewhere
}
I would add it to the beginning and end, so you don't match foocontact
either. 我会将其添加到开头和结尾,因此您也不匹配
foocontact
。 Or you could make sure that a slash precedes contact. 或者,您可以确保在联系之前加斜杠。
if ($uri =~ m|/contact\b|i ) {
## Redirect somewhere
}
Try: 尝试:
echo -e "/contacts/qqq\n/contact\n/contact/talk\n/ConTact\n/contactx/"|
perl -ne 'm{/contact(?:/|$)} and print'
Output: 输出:
/contact
/contact/talk
/^\/contact\//
涉及一些猜测-您的网址似乎以/contact
开头,以防万一,请删除初始插入符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.