# 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 founder, Robbin Steif, started LunaMetrics in 2004. 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 winner of a BusinessWomen First award, as well as a Diamond Award for business leadership. In 2017, Robbin sold her company to HS2 Solutions and has since retired from LunaMetrics.

• Bhagawat

Hi Robbin,

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

Both will function the same!

Thanks,
Bhagawat.

• 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

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.

1.877.220.LUNA

1.412.381.5500

getinfo@lunametrics.com

Questions?
We'll get back to you
Our Locations
THE FOUNDRY [map]

24 S. 18th Street
Suite 100

Pittsburgh, PA 15203

THE STUDIO [map]

4115 N. Ravenswood
Suite 101
Chicago, IL 60613

THE LODGE [map]

2100 Manchester Rd.
Building C, Suite 1750
Wheaton, IL 60187