Upcoming LunaMetrics Seminars
Washington DC, Sep 22-26 Boston, Oct 6-10 Chicago, Oct 20-24 Seattle, Nov 3-7

# Regular Expressions Part VII: (Parenthesis)

As promised, here is installment VII of my Regular Expressions (RegEx) tutorial – parenthesis. I am learning and sharing at the same time. I am only learning them to use for Google Analytics.

I wanted to get this one out soon after my last RegEx post, because the last one was on the use of pipes, which stand for OR in Regular Expressions. Pipes (OR symbols) and parenthesis often go together.

My tutor, Steve in Australia, does a really good job of explaining parenthesis. In the same way that this mathematical statement,-

6*(2+3)

is equivalent to 6*2 plus 6*3, parenthesis in Regular Expressions make sure that the stuff outside of the parenthesis get applied to the stuff inside of the parenthesis equally.

For example — and remembering that the pipe symbol | stands for OR — we can have a regular expression like this:

grand(mother|father)

That will match either grandmother or grandfather.

Or, here is another, similar but not identical example:

Ste(ph|v)en

that will match either Stephen or Steven

What if the two terms are really different and there isn’t much in the way of grouping to do? For example, what if we want to filter out Robbin or Luna (which I do all the time in my GA)? Then we can go back to the last lesson on OR and just use a simple pipe:

Robbin|Luna

(Often, even people who know me well misspell my name, so I could use what I learned in lesson V, question marks, to make the second “b” optional, like this: Robb?in|Luna)

In Google Analytics (I won’t speak to other languages) we don’t need to use any parenthesis if there isn’t any grouping — the pipe can stand on its own. Or as Justin always tells me, keep it simple.

[Incredibly techie addition: My last comment about never needing parenthesis when there is nothing outside the parenthesis is not always true. At the eMetrics Summit, Nick from Google and Justin from Epikone taught me a lot about creating custom filters and during that process, explained how parenthesis define a variable. I will revisit this topic later.]

Robbin

Our owner and CEO, Robbin Steif, started LunaMetrics ten years ago. She is a graduate of Harvard College and the Harvard Business School, and has served on the Board of Directors for the Digital Analytics Association. Robbin is a recent winner of a BusinessWomen First award, as well as a Diamond Award for business leadership.

http://www.lunametrics.com/blog/2006/10/12/regular-expressions-part-vii-parenthesis/

## 4 Responses to “Regular Expressions Part VII: (Parenthesis)”

Bhagawat says:

Hi Robbin,

You can use Rob?bin instead of Robb?in, right?

Both will function the same!

Thanks,
Bhagawat.

Robbin says:

Right you are, you can do it either way.

Robbin, I came to this site for some help with regex and found a treasure trove of goodies. I know the regex posts are pretty old, but still relevant and very useful.

FWIW, after much sidetracking, I used your regex posts to figure out exactly what I needed (I was using [] instead of () in my statement) and bookmarked for more reading.

Thank you to you and the rest of your team for these blog posts, they’re much appreciated!

Robbin says:

So glad you enjoy. I used to feel bad (years and years ago) when no one ever commented, but gradually, a whole group of comments evolved.