Regular Expressions Part IV: The ending Anchor $/
September 14, 2006
As regular readers know, I am learning about and sharing my lessons on Regular Expressions. In my last post on this topic, Part III, I wrote about the opening anchor, aka the carat symbol. It’s used at the beginning of some regular expressions. Today I am writing about its sister, this anchor $, which is sometimes used at the end of Regular Expressions (RegExen;-)
$: match to the end of the field
What they really mean is, don’t match if the string from my website has any characters beyond where I have placed the dollar sign in my Regular Expression. The dollar sign signals all the characters that I want to match to. (This is the hard part about the explanation, it’s always hard to explain what the target is and what the RegEx is.)
So let’s say that you have some pages that end in htm and others in html. You want to write a Google Analytics Step 1 (part of a goal) for your email sign-up form, but you only want the new .htm version. Your RegEx might look like this:
The dollar sign tells the Google Analytics, if the page on your site has anything after the final “m” in “htm,” it doesn’t count as a match to this expression. Notice that I also used a backslash before the dot so that Google Analytics interprets it just as a dot, not as anything special.
I understood what it does pretty quickly, but I had to put this post off for at least a week in an effort to understand why anyone would ever use a dollar sign. Here’s the problem: if you have any campaign code whatsoever that gets attached to this string when someone lands on your site, there is no match. For example, let’s say someone lands on your site and when he looks up at the address bar, it says this:
As soon as there is campaign code, the dollar sign anchor will tell GA that there is no match. And it can be hard to remember everywhere that you would have campaign code and everywhere that you wouldn’t.
So forget the fanciful examples – why would anyone ever use it?
Once place you might use it would be with an IP address (for a filter, someone that you are trying to filter in or filter out.) You might have an IP addresss like this that you are screening out: 18.104.22.168, which matches 22.214.171.124, but you want to be sure that it doesn’t match 126.96.36.1999 — so you set up your expression to be 188.8.131.52$ . And if you want to be sure that it doesn’t match 5184.108.40.206 as well, you should use the beginning anchor ^ (how’s that for a reminder and a link back to the last part of this series), ^220.127.116.11$
You could also use it with the “on the fly” Advanced Filter that you can do with every report (the little box near the top of every screen.) For example, if I wanted to know how many different variations of search terms end with my company’s name, I should be able to type in LunaMetrics$ in that little box on one of the search term reports (like Marketing Optimization > Search Engine Marketing > Overall Keyword conversion, and only see search terms like LunaMetrics or Call LunaMetrics or who the heck is LunaMetrics). Now that we don’t see that many keywords anymore, this particular example is not as helpful, but there are many ways to filter your data. RegEx will always be your friend.
And you can use it in the profile filters.
Am hoping one of the GA gurus will get on to say why the $ anchor doesn’t work in the in-report filters. After all, I am only taking lessons here, and sharing them with the world.
Dollars signs $
Question marks ?
Square brackets and dashes –
Plus signs +
Regular Expressions for Google Analytics: Now let’s Practice
RegEx and Good Greed
updated March 29, 2015