3

I need to come up with a regular expression that matches all page paths that:

  • starts with /google+redesign/ AND
  • ends with /quickview.

What would a correct regex combination look like?

0

1 Answer 1

5

Google Analytics uses the RE2 flavor of regex.


First, I'll go through some examples to lay down the building blocks. This will help you understand. After, we'll put it all together to create a solution that captures all urls with the paths you've specified.

Example 1

This regular expression will match all urls that have a path starting with /foo/ and ending with /bar.

(http|https):\/\/www.example.com\/foo\/[^\/]+\/bar$

Here is how it works:

  • Our 1st Capturing Group is (http|https)
  • Inside our capturing group http is our 1st Alternative - http matches the characters http literally (case sensitive)
  • The | means "or"
  • Our 2nd Alternative is https - https matches the characters https literally (case sensitive)
  • : matches the character : literally (case sensitive) and then the following 2 \/'s are our // - a \ is how characters are escaped regex.

Important to note is that the above expression does not control for more than one path between our targeted slugs. So what can we do?

Example 2

Outside of regex, an asterisk * is commonly thought of as a wildcard, implying zero or more instances of any character. In a regular expression, however, the asterisk is a metacharacter for zero or more instances of the preceding character.

This regular expression helps avoid the pitfalls of the last one allowing us to get a successful match even if there's more than one sub-directory between your start and end.

.*/foo/.*/bar$

How this one works:

  • Our first . matches any character (except for line terminators)
  • * matches the previous token between zero and unlimited times, as many times as possible, giving back as needed
  • /foo/ matches the characters /foo/ literally (case sensitive)
  • The cycle then repeats itself until /bar/ is found and then we end our expression with a $

Quick note on escaping

If the idea of "escaping" in the previous example sounded confusing consider this:

In my second bullet above I had to escape the * so that it didn't create two bullet points, since our markdown permits it in addition to + and - ) when creating unordered/bulleted lists.

Unescaped, I'd have this:

    • matches the...

Now Let's Match Your Specific Path

To reiterate, you need to match urls with paths that start with /google+redesign/ AND end with /quickview. Since there's a + that will need to be escaped.

Here's a couple ways using both of the approaches we've taken so far:

First

.*/(google\+redesign)/.*/(quickview)$

screenshot from regex101.com showing this first regex solution working

Second

(http|https):\/\/www.example.com\/(google\+redesign)/.*/(quickview)$

screenshot from regex101.com showing the second regex solution working

You could also replace your first capturing group with a "non-capturing group" if, for example, you don't care about the protocol (http vs https):

(?:http|https):\/\/(www).example.com\/(google\+redesign)/.*/(quickview)$

By including the ?:, in our first group becomes non-capturing. The parser still uses the group to match the text, but it's ignored later in the final result.

Hopefully this helps. Regex is super confusing to wrap your head around when you're just diving into it.

I recommend using Regex101, it is a fantastic tool to use when constructing regular expressions. It will also help you learn. It defaults to PCRE, but you can switch to RE2 by selecting Golang on the left hand panel. The Go Programming Language uses it.

1
  • 2
    Good evening from sweden, Mike. I've just sat down and read through your answer and this clarified alot for me, you explination was very detailed and easy to understand. Especially as a newbie to the whole world of regex. You deserve a big thank you for taking the time to answer. Have a great rest of the week, cheers. Jan 26 at 19:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.