Regex to Match Number of Subdirectories in a URL


  1. Hi, thanks for writing out this article. It’s been super helpful and is almost exactly what I’m looking for.

    However, I’d like to take “Variation 1” one step further.

    With the example, Regex for exactly one sub-directory:

    This will match any top-level directory, for example: /retail/.

    I’d like to do this, plus 2 and 3 directories deeper, but ideally I want to specify exactly what that directory is. In this example, matching only the /retail/ directory, and all subsequent subdirectories.

    Would love if you’d be able to explain that!

    1. That’s great, glad to hear it was useful!

      If you want to specify a specific directory, you should be able to type it in like this:

      ^/(retail)+/[^/]+/$ (2 subdirectories, including /retail/)
      ^/(retail)+/[^/]+/[^/]+/$ (3 subdirectories, including /retail/)
      ^/(retail)+/[^/]+/[^/]+/[^/]+/$ (4 subdirectories, including /retail/)

      Please try that and let me know if there’s any issue.

      1. Hi there! Thanks so much for this article. It’s incredibly helpful. I’m trying the above without luck. In my example there is an underscore in the first subdirectory I’d like to group the content by.
        Goal: group all content containing two sub directories after /retail_store/, beginning with /retail_store/

        Any idea what I might be doing wrong?

      2. Thank you for your comment, much appreciated!

        If you have 2 subdirectories after /retail_store/, you’ll have a total of 3 subdirectories. So for that case you should use the following:

        It shouldn’t matter if there’s an underscore or not. Please try it out and let me know how it goes!

  2. Thanks so much, Ana! I’m still having some issues with this.

    For context, I’m trying to group content in GA using this regex. I want to group views to two different sub-directories:
    Pages with exactly two subdirectories starting with retail_store is one category, and pages with exactly three subdirectories starting with retail_store is the second category. Any other advice you might have would be awesome – I so appreciate your help!

      1. Hey there! Just realized I never received the notification for this. Your suggestion works! I’ll do some reading up around trailing slashes to make sure I don’t run into this issue again in the future. 🙂

  3. Super helpful post! Do you also have something in mind for filtering a Landing Page URL based on the amount of certain symbols. E.x. filter out URLs that countain 3 or more “_” ?

    1. I think you can use the following regex to capture URLs with 3 or more underscores:
      Please check it out and let me know if that works for you!

    1. Hey Himanshu! Thank you for the comment.

      In this case you should be able to make a regex like this:

      (the pipe character means OR).

      Does that work for you?

    1. Hi Roberto, if you just want to group by those specific pages in GA I don’t think you need regex at all, you can just make conditions like Page contains /landingpage and Page contains /usermanagement. Let me know if I’ve misunderstood what you’re looking for.

      1. Hi Ana,
        Thanks a lot for you feedback. I tried to follow your suggestion but it doesn’t work anyway.

        What could I do?

  4. Hi Ana – great article! Question.

    Is there a way to grab the beginning and end by required URL string, but disregard the amount of subdirectory paths?

    Example: and

    I want to grab the URL string if both /abc/ and /123 are True, regardless of how many subdirectory paths there are. Does this make sense? Thanks in advance for any direction you can provide!

    1. Hey AF, sure, you can use .* as a wildcard.

      So if you want to match on /abc/ and /123, you’d use the following expression: /abc/.*123

      (I left the slash off the 123 so the regex will work even if it’s immediately following /abc/.You can test it in your All Pages report to make sure it pulls in the URLs you’re looking for.)

    1. Thank you for sharing! Though, your regex will only work for the specific case where the URL ends with some known string (like your ‘.aspx’ example). If you want something a little more generalizable I think you could use this to match no directories: ^/[^/]+[a-zA-Z0-9]$

Leave a Reply

Your email address will not be published.