Reputation: 1211
I need to write regex for Perl so that it matches /contact
(and everything beyond so /contact/talk etc.).
But I also have URL for /contacts
so I want it to skip this.
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.
Upvotes: 0
Views: 270
Reputation: 7610
Try:
echo -e "/contacts/qqq\n/contact\n/contact/talk\n/ConTact\n/contactx/"|
perl -ne 'm{/contact(?:/|$)} and print'
Output:
/contact
/contact/talk
Upvotes: 0
Reputation: 17238
/^\/contact\//
there is some guesswork involved - it appears that your urls start with /contact
, in case they don't, drop the initial caret.
Upvotes: 0
Reputation:
\b
is a word boundary marker, which prevents this match from being in the middle of a word:
if ($uri =~ m|\bcontact\b|i ) {
## Redirect somewhere
}
I would add it to the beginning and end, so you don't match foocontact
either. Or you could make sure that a slash precedes contact.
if ($uri =~ m|/contact\b|i ) {
## Redirect somewhere
}
Upvotes: 0
Reputation: 8293
if ($uri =~ m/\/contact(?:$|\/)/i ) {
## Redirect somewhere
}
Will match /contact
because of the $
, and everything like /contact/...
Upvotes: 0