I am trying to write a regular expression which will check a URL contains certain words and excludes another.
The reason for this is I am trying to track traffic moving through my website and I don't want to count anyone who hits the Thank You page.
So for example:
http://www.mywebsite.com/register-now/
- MATCHhttp://www.mywebsite.com/contact-us/
- MATCHhttp://www.mywebsite.com/register-now/thank-you
- NO MATCHhttp://www.mywebsite.com/contact-us/thank-you
- NO MATCHhttp://www.mywebsite.com/thank-you
- NO MATCHI have 2 words (register-now
and contact-us
) these must be in the URL. However I must ensure that 1 word (thank-you
) is also not in the URL.
I have tried to use a negative lookahead to check that the URL does NOT contain thank-you but It is not working:
"^(?!.*\/thank\-you+)\/(contact\-us|register\-now)\/.*"
To represent this, we use a similar expression that excludes specific characters using the square brackets and the ^ (hat). For example, the pattern [^abc] will match any single character except for the letters a, b, or c.
. means match any character in regular expressions. * means zero or more occurrences of the SINGLE regex preceding it.
Definition and Usage The \f metacharacter matches form feed characters.
To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.
In a single regex you can use negative lookahead:
^(?!.*\/thank-you(?:\/|$))(?:.*\/)?(?:contact-us|register-now)\/
RegEx Demo
(?!.*\/thank-you(?:\/|$))
is negative lookahead that will fail the match if URL has /thanks-you
or /thank-you/
.MULTILINE
mode if your text contains multiple URLs in each line.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With