Question marks are used to match or ignore the last item
in your regular expressions. In most cases ‘last item’ means a character, but it can be a group of characters and/or regex code in brackets.
In practice, this is a useful little catch-all filter for use under specific circumstances where you see small variations in your reports that you want to group.
For example, working in 22 languages as I do, I can’t always rely on our affiliate partners to faithfully reproduce the campaign tracking codes we request from them.
I have seen all of the following
values as a ‘medium’ in my traffic sources reports.
Affiliate (*This is the correct one!)
Thankfully, we can cover all of these with one bit of regex using question marks!
The regex for the above would be: ‘Aff?ill?iate?’
The question mark is great for filtering single and plural versions of the same term.
In my case this might be ‘colleges?’ to match both college and colleges.
Another great example is if you need to filter for keywords that are very similar, or keywords with deliberate mis-spellings (for the international market)
We have a free online english test, so we see ‘test ingles’ and ‘test inglese’ in our terms. The regex for this would be ‘test inglese?’.
You can also optionally exclude whole reams of regex or characters with brackets. I don’t use this as much in practice, but it works in exactly the same way.
Perhaps your company has a domain strategy like Virgin’s, where you have multiple sites covering different verticals but all referring traffic to one another.
You may want to filter for:
You can do this with brackets, pipes, and a question mark!
The full stop above is a special character in regex. It doesn’t cause us an issue in the example above, but I think it’s time we had a chat about the backslash!
Links to the rest of the series:
RegEx 1: Introduction
RegEx 2: Pipe
RegEx 3: Brackets
RegEx 4: Question Mark – You are here!
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