Regular Expressions Pt. V: Question Marks ?

September 25, 2006
By Robbin Steif

So I continue here with my Regular Expressions (“RegEx”) lessons. I am learning RegEx only because so many customers use Google Analytics, which throws the code at the customer with very little explanation.


This next lesson is about the question mark. This time, Google does a pretty good job of meaning what they say:

? Match zero or one of the previous expression

When they say, “The previous expression,” they mean, the character that comes right before the question mark. Since that is still pretty opaque, let me shine some light here.

Let’s say that you have an economics website and you only want to look at the referrers that have the word “labor” in their title. But some of those referrers come from non-US countries where they spell it “labour.” You could create a filter like this: labou?r

That way, it will match “labour” (which does have a “u,” which is the previous expression) and labor (which has zero of the previous expression, i.e. no “u” is included.)

You cannot use it like this : labo?r, or at least, not for the same purpose. It’s not a wildcard that you stick in between the o and the r to match any letter. The only matches would be to “labor” (zero of the previous expression) and “labr” (Thanks Serge for catching my error.)

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
Minimal Matching