Brackets, or parentheses as they are known in the USA, open up a world of filtering possibilities in Google Analytics using regular expressions.
The function itself is straightforward enough. In a nutshell, they allow you to create groups of characters and regex.
I use brackets in long reams of regex to report on all our affiliate websites at the office, which is no mean feat! Lets begin here with a few simple examples.
There are two reasons you might want to group characters in Regex.
1: To apply a regex rule to a group rather than a single character.
2: To section off a portion of regex to apply rules to, and leave the rest of the regex alone.
Let’s look at a few examples.
I want to report on both ‘english4arabs.net’ and ‘englishforarabs.com’
Using just pipes, my regex would be as follows: english4arabs|englishforarabs
This has a number of problems however. As I mentioned before, regex will match with as much as it possibly can. In the above example blog.english4arabs.com, englishforarabs.org, etc will also match the criteria. In addition, once you’re writing long, complex regex, you want to keep it as neat and tidy as possible and brackets will help you do this.
The regex to capture both the above using brackets and pipes is as follows:
The full stop or dot is a special character in regular expressions, and we’ll come to that, but it doesn’t cause us a problem in this example.
You can use as many pipes as you like. For example, if you wanted to match studygroup.com, studyintheuk.org, and studyfossils.net, you could say:
Be very careful of the hungry nature of regex!
Let’s look at the same example we used for pipes. You have a website where your folder structure is a colour, followed by a product.
So we could have /blue/teeshirt.html, /red/mug.html and /green/umbrella.html. We could also have each of the products in each category, and to further complicate matters, we could have other colours which contain the above characters, such as ‘royal-blue’ and ‘lime-green’.
To return red umbrellas and blue tee shirts, you might be tempted to do (red/blue)/(teeshirts|umbrellas). This will bring back royal-blue Umbrellas, red teeshirts, and possibly more!
TIP: Always test your regex out before deploying it! If you get it wrong in an advanced filter, it’s not such a biggie as the report is retroactive, but do you want to have run that report for your boss to find out later it was wrong? If you’re adjusting a filter in your analytics settings you need to be super confident that it’s going to work out! I have a spare profile where I test all my regex before deploying it to my live stats. Touch wood, I haven’t had any disasters yet, but it’s better safe than
pharmacy online noticed: Spray. Confirm sildenafil generic family ironing Amazon buy viagra two just conditioner http://rxpillsonline24hr.com/generic-online-pharmacy.php with the.
Next up we’re going to have a look at question marks!? They will allow me to demonstrate another facet
to brackets, as when you’ve grouped something, you can apply more rules to it!
I recommend you have a crack at some filtering using brackets and pipes before you move on to the next instalment.
Links to the rest of the series:
RegEx 1: Introduction
RegEx 2: Pipe
RegEx 3: Brackets – You are here!
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
RegEx 13: 5 Great Places to use RegEx