The Dollar Sign is a really handy regular expression for situations where you have precise strings you want to capture.
The expression means don’t match if the string has any characters beyond this point. When used with the Caret, you can precisely pinpoint the exact string you want to capture and not match anything that has characters before or after what you have specified.
Lets look at a few practical examples of where you might use the dollar sign.
Dollar Sign: examples of use
I have a category on my site called regular expressions. In my content reports this appears as: ‘/how-to/regular-expressions/’. All the pages in the category begin with this, however, so the page you’re reading now shows up in Google Analytics as ‘/how-to/regular-expressions/dollar-sign-google-analytics’.
So if I just want to look at the category page, my RegEx would be:
If you’re using campaign tracking, you might also find the dollar sign useful. For example, we set the source as ‘facebook’ for some campaigns we track using campaign tracking.
One result of this is in the ‘source’ report. I may see any of the following:
‘facebook$’ will return just ‘facebook’ from the above, as both the other strings are ruled out by the ‘.com’, however, if I am looking for a precise string, and I know what I want from beginning to end, I would always use the RegEx to ensure I get the right result.
that would be ‘^facebook$’
This costs you no time and is just a great habit to get into. Even though I’m 99.9% sure I’m not going to have any ‘facebook’s with a different prefix, there is absolutely no harm in making sure.
Can you think of any other great examples where a dollar sign is indispensable? I’d love to hear about your experiences with RegEx.
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 – You are here!
RegEx 12: Square Brackets
RegEx 13: 5 Great Places to use RegEx