13.07.2015 Views

ebook on regular expressions for Google Analytics - LunaMetrics

ebook on regular expressions for Google Analytics - LunaMetrics

ebook on regular expressions for Google Analytics - LunaMetrics

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

leT s prActicEWhen I was a newbie to RegEx, I used to see this <strong>on</strong> the RegEx Wikipedialisting (which I pored over c<strong>on</strong>stantly, trying to understand what these little characters were):((great )*grand)?((fa|mo)ther)Like most things in life, <strong>on</strong>ce you understand them, they are trivial … but be<strong>for</strong>e you understand them, they are daunting. So I will take this apart tomake it easier to understand.The parentheses create groups, separated by a questi<strong>on</strong> mark. So we effectively have:(First Expressi<strong>on</strong> in the first set of parentheses)?(Sec<strong>on</strong>d Expressi<strong>on</strong> in the sec<strong>on</strong>d set of parentheses)Since a questi<strong>on</strong> mark means include zero or <strong>on</strong>e of the <strong>for</strong>meritem, we know that the target string will be a match to this practiceRegEx whether it <strong>on</strong>ly matches the expressi<strong>on</strong> in the sec<strong>on</strong>d setof parentheses or it matches both the expressi<strong>on</strong>s in both sets ofparentheses. (Right? That’s what questi<strong>on</strong> marks do, allowing thetarget string to match the item be<strong>for</strong>e them OR NOT.) So let’s startby looking at the sec<strong>on</strong>d half <strong>on</strong>ly, which we now know should beable to stand by itself:((fa|mo)ther)The pipe symbol | means or. So this resolves to father or mother.Now let’s go back andlook at the first half, thepart that came be<strong>for</strong>ethe questi<strong>on</strong> mark:((great)*grand)The star tells us to match zero, <strong>on</strong>e or more than <strong>on</strong>einstances of the expressi<strong>on</strong> be<strong>for</strong>e it. So it can matcha string that doesn’t include great, in which case wejust have grand; and, of course, we always have theend of the expressi<strong>on</strong>, which will be either mother orfather. So we are allowed to match to grandmotheror grandfather. It can match a string which includesgreat just <strong>on</strong>ce, in which case we have greatgrandmother or great grandfather. And it canmatch a string which includes great more than <strong>on</strong>ce,so we might end up with great great great greatgrandmother or great great grandfather.So there you have it – all your ancestorswith just <strong>on</strong>e Regular Expressi<strong>on</strong>.21

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!