Square brackets can be used with dashes to match with a range, or sequential list of letters
or numbers. If you’re tracking a blog, for example, or a website that doesn’t have lists of products organised by sequential code numbers, you probably won’t have much need of it.
I have seen this used to exclude a number of related IPs, so there are a few other applications! Read the below and see if you can find a use for the square brackets and dash.
What do square brackets and dashes do?
[a-z] will match any lowercase letter in English.
[A-Z] will match any capital letter.
[0-9] will match any single digit number.
You can use a combination of the above, but don’t use a separator, for example, to match any letter, you could use [a-zA-Z]
Just like square brackets on their own, with a dash they will only match one character in the range. So [1-2] will match 1 and 2, but not 12.
Examples of using square brackets and dashes.
Lets say you have an e-commerce site that uses page ids, or other ids, to catalog your products.
You might have values like mysite.com/products.html?product=100.
If your products are organised, say CDs are 100-199 and DVDs are 200-299, square brackets and dashes could be very useful for filtering!
In this example you could use the following regex:
(the highlighted regex is the key here, this will match 100, 199 and every combination in between)
Square brackets can also be used without dashes
You can put a list of English letters or characters into brackets to return any of them.
had a funnel on your site as follows:
You could match all of these pages with the following:
These don’t have to be in a sequence, if you wanted to just look at pages 1 and 3, you could use the following:
An example using letters might be:
This would match with ‘spot’ and ‘spat’, but not with ‘sport’ or ‘sprat’. The RegEx will only take one character from the list inside the square brackets.
That is chapter and verse on square brackets and dashes! Was it useful? I’d love to hear from you below…
Links to the rest of the series:
RegEx 1: Introduction
RegEx 2: Pipe
RegEx 3: Brackets
RegEx 4: Question Mark
RegEx 5: Backslash
RegEx 6: Plus Sign
RegEx 7: Dot
RegEx 8: Star
RegEx 9: Dot Star
RegEx 10: Caret
RegEx 11: Dollar Sign
RegEx 12: Square Brackets – You are here!
RegEx 13: 5 Great Places to use RegEx