Regular Expressions Pt. V: Question Marks ?

By /

September 25, 2006

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

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 winner of a BusinessWomen First award, as well as a recent Diamond Award for business leadership. You should read her letter before you decide to work with us.

  • Serge

    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?

  • http://www.lunametrics.com/blog LunaMetrics Blog

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

  • Pingback: Regular Expressions for Google Analytics Part VII: (Parenthesis)()

Contact Us.

LunaMetrics

24 S. 18th Street, Suite 100,
Pittsburgh, PA 15203

Follow Us

1.877.220.LUNA

1.412.381.5500

getinfo@lunametrics.com

Questions?
We'll get back to you
in ONE business day.