Regular Expressions Part IV: The Ending Anchor $
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:
/email-form.htm$
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:
/email-form.htm?cid=123
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: 12.34.56.78, which matches 12.34.56.78, but you want to be sure that it doesn’t match 12.34.56.789 — so you set up your expression to be 12.34.56.78$ . And if you want to be sure that it doesn’t match 512.34.56.78 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), ^12.34.56.78$
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.
Backslashes
Dots .
Carats ^
Dollars signs $
Question marks ?
Pipes |
Parentheses ()
Square brackets []and dashes –
Plus signs +
Stars *
Regular Expressions for Google Analytics: Now let’s Practice
Bad Greed
RegEx and Good Greed
{Braces}
Minimal Matching
Lookahead
Robbin
updated March 29, 2015