Regular expression is a powerful way to target complicated URLs or to simplify complex conditions into a single query.
How does a regular expression (regex) work?
In regular expression you combine literal characters with special characters to create a logical pattern. This logical pattern then defines which URLs will be targeted and which will be out of scope.
- Literal character = any character of your URL, such as "w" or "/" from "www.examplepage.com/page"
- Special character = a character having special meaning, such as "\d" means represents any single digit
Please note, that some characters common in the URL (such as / or .) is by considered special by regex. Therefore if they are meant literally, it is necessary to always put "\" in front of them. The common URL will then look like "www\.examplepage\.com\/page".
Examples of regular expressions
Regular expression | URLs that will be targeted | More information |
^www\.examplepage\.com$ | An exact match of www.examplepage.com. URLs containing specific pages (such as www.examplepage.com/page) will be excluded! | ^ signals that the URL must start there $ signals that the URL must end there |
examplepage\.com\/id\/\d+\/edit | examplepage.com/id/<number>/edit All URLs containing "examplepage.com/id/", then number of any length and then "/edit" will be targeted | \d represents any digit + signals the previous sign is present one or more times Together (\d+) they mean one or more digits. |
examplepage\.(com|net) | Any URL containing examplepage.com or examplepage.net | | signals that word before or word after needs to be there The condition can be put inside of ( ) for better readability. |
Special characters (essentials)
^ | start of string |
$ | end of string |
a+ | one or more of a |
a* | zero or more of a |
a? | zero or one of a |
a{3} | exactly 3x a |
a{3,} | 3x or more of a |
a{3,5} | Between 3x and 5x of a |
a|b | a or b |
\d | any digit |
\D | any non-digit |
. | any single character |
[abc] | a single character from a, b or c |
[^abc] | a single character except a, b or c |
[a-z] | a single character in range from a to z |
Testing your regular expressions
You can use the tool https://regex101.com/ to test your regular expressions.