Upcoming LunaMetrics Seminars
Los Angeles - Anaheim, Sep 8-12 Washington DC, Sep 22-26 Boston, Oct 6-10 Chicago, Oct 20-24

Regular Expressions Pt. V: Question Marks ?





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.

regular-expressions

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.)

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

Robbin Steif

About Robbin Steif

Our owner and CEO, Robbin Steif, is an analyst herself. 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 (formerly the Web Analytics Association.)

http://www.lunametrics.com/blog/2006/09/25/regular-expressions-pt-v-question-marks/

2 Responses to “Regular Expressions Pt. V: Question Marks ?”

Serge says:

Hi Robbin,

Pardon me but I think you may be wrong in that last paragraph:
“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 “laboor” (gibberish, but still meets the criteria, i.e. one of the previous expression, which is now an “o”.)”

If, as you mention it above, ? matches zero or one of the previous expression, then labo?r matches labr or labor, doesn’t it?

You are right!! I republished (or will in a minute) – thanks!! Rpbbom