Regex to Match Number of Subdirectories in a URL












0















I found an article that almost covered my exact need. However, it needs to be adjusted a bit and I can't quite figure it out.



VARIATION 1: EXACTLY X NUMBERS OF SUB-DIRECTORIES, WITH TRAILING SLASH
This variation assumes each subdirectory ends in a trailing slash.



Regex for exactly one sub-directory
^/[^/]+/$



example matching URL path: /retail/



Regex for exactly two sub-directories
^/[^/]+/[^/]+/$



example matching URL path: /retail/clothing/



I would like to adjust these two rules to match a specific directory.



In my case, I have a site that has multiple top-level directories (products and stores, each with 2 additionally subdirectories).



I would like to create a regex rule to only target /products/ plus the following 3 subdirectories.










share|improve this question


















  • 1





    What regex engine are you using? Are you using a specific language?

    – Conner
    Nov 21 '18 at 4:51











  • I'm trying to get this to work in Google Analytics.

    – Leigh
    Nov 21 '18 at 6:02











  • support.google.com/analytics/answer/1034324?hl=en It appears Google Analytics doesn't support {3}. You could submit a feature request.

    – Conner
    Nov 21 '18 at 6:05


















0















I found an article that almost covered my exact need. However, it needs to be adjusted a bit and I can't quite figure it out.



VARIATION 1: EXACTLY X NUMBERS OF SUB-DIRECTORIES, WITH TRAILING SLASH
This variation assumes each subdirectory ends in a trailing slash.



Regex for exactly one sub-directory
^/[^/]+/$



example matching URL path: /retail/



Regex for exactly two sub-directories
^/[^/]+/[^/]+/$



example matching URL path: /retail/clothing/



I would like to adjust these two rules to match a specific directory.



In my case, I have a site that has multiple top-level directories (products and stores, each with 2 additionally subdirectories).



I would like to create a regex rule to only target /products/ plus the following 3 subdirectories.










share|improve this question


















  • 1





    What regex engine are you using? Are you using a specific language?

    – Conner
    Nov 21 '18 at 4:51











  • I'm trying to get this to work in Google Analytics.

    – Leigh
    Nov 21 '18 at 6:02











  • support.google.com/analytics/answer/1034324?hl=en It appears Google Analytics doesn't support {3}. You could submit a feature request.

    – Conner
    Nov 21 '18 at 6:05
















0












0








0








I found an article that almost covered my exact need. However, it needs to be adjusted a bit and I can't quite figure it out.



VARIATION 1: EXACTLY X NUMBERS OF SUB-DIRECTORIES, WITH TRAILING SLASH
This variation assumes each subdirectory ends in a trailing slash.



Regex for exactly one sub-directory
^/[^/]+/$



example matching URL path: /retail/



Regex for exactly two sub-directories
^/[^/]+/[^/]+/$



example matching URL path: /retail/clothing/



I would like to adjust these two rules to match a specific directory.



In my case, I have a site that has multiple top-level directories (products and stores, each with 2 additionally subdirectories).



I would like to create a regex rule to only target /products/ plus the following 3 subdirectories.










share|improve this question














I found an article that almost covered my exact need. However, it needs to be adjusted a bit and I can't quite figure it out.



VARIATION 1: EXACTLY X NUMBERS OF SUB-DIRECTORIES, WITH TRAILING SLASH
This variation assumes each subdirectory ends in a trailing slash.



Regex for exactly one sub-directory
^/[^/]+/$



example matching URL path: /retail/



Regex for exactly two sub-directories
^/[^/]+/[^/]+/$



example matching URL path: /retail/clothing/



I would like to adjust these two rules to match a specific directory.



In my case, I have a site that has multiple top-level directories (products and stores, each with 2 additionally subdirectories).



I would like to create a regex rule to only target /products/ plus the following 3 subdirectories.







regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 4:40









LeighLeigh

31




31








  • 1





    What regex engine are you using? Are you using a specific language?

    – Conner
    Nov 21 '18 at 4:51











  • I'm trying to get this to work in Google Analytics.

    – Leigh
    Nov 21 '18 at 6:02











  • support.google.com/analytics/answer/1034324?hl=en It appears Google Analytics doesn't support {3}. You could submit a feature request.

    – Conner
    Nov 21 '18 at 6:05
















  • 1





    What regex engine are you using? Are you using a specific language?

    – Conner
    Nov 21 '18 at 4:51











  • I'm trying to get this to work in Google Analytics.

    – Leigh
    Nov 21 '18 at 6:02











  • support.google.com/analytics/answer/1034324?hl=en It appears Google Analytics doesn't support {3}. You could submit a feature request.

    – Conner
    Nov 21 '18 at 6:05










1




1





What regex engine are you using? Are you using a specific language?

– Conner
Nov 21 '18 at 4:51





What regex engine are you using? Are you using a specific language?

– Conner
Nov 21 '18 at 4:51













I'm trying to get this to work in Google Analytics.

– Leigh
Nov 21 '18 at 6:02





I'm trying to get this to work in Google Analytics.

– Leigh
Nov 21 '18 at 6:02













support.google.com/analytics/answer/1034324?hl=en It appears Google Analytics doesn't support {3}. You could submit a feature request.

– Conner
Nov 21 '18 at 6:05







support.google.com/analytics/answer/1034324?hl=en It appears Google Analytics doesn't support {3}. You could submit a feature request.

– Conner
Nov 21 '18 at 6:05














1 Answer
1






active

oldest

votes


















1














I don’t have a system to test this, but I think you can use the ‘?’ and parens to add the variable sub-dirs on your base match for “/products/“.



The base regex would be “^/products/$”.
Then to match 0 or 1 subdirectories, you could add “([^/]+/)?”
Add three of these and I think you have the regex to match your base plus 0 to 3 subdirectories. I think that is what you were asking for.



^/products/([^/]+/)?([^/]+/)?([^/]+/)?$


The equivalent RE worked in Emacs for these test lines:



/products/
/products/a/
/products/a/b/
/products/a/b/c/


And did not match this:



/products/a/b/c/d/


If you always wanted the 3 subdirectories, drop the ‘?’s to make the extras required.



^/products/[^/]+/[^/]+/[^/]+/$


Hope this helps!






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405369%2fregex-to-match-number-of-subdirectories-in-a-url%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I don’t have a system to test this, but I think you can use the ‘?’ and parens to add the variable sub-dirs on your base match for “/products/“.



    The base regex would be “^/products/$”.
    Then to match 0 or 1 subdirectories, you could add “([^/]+/)?”
    Add three of these and I think you have the regex to match your base plus 0 to 3 subdirectories. I think that is what you were asking for.



    ^/products/([^/]+/)?([^/]+/)?([^/]+/)?$


    The equivalent RE worked in Emacs for these test lines:



    /products/
    /products/a/
    /products/a/b/
    /products/a/b/c/


    And did not match this:



    /products/a/b/c/d/


    If you always wanted the 3 subdirectories, drop the ‘?’s to make the extras required.



    ^/products/[^/]+/[^/]+/[^/]+/$


    Hope this helps!






    share|improve this answer




























      1














      I don’t have a system to test this, but I think you can use the ‘?’ and parens to add the variable sub-dirs on your base match for “/products/“.



      The base regex would be “^/products/$”.
      Then to match 0 or 1 subdirectories, you could add “([^/]+/)?”
      Add three of these and I think you have the regex to match your base plus 0 to 3 subdirectories. I think that is what you were asking for.



      ^/products/([^/]+/)?([^/]+/)?([^/]+/)?$


      The equivalent RE worked in Emacs for these test lines:



      /products/
      /products/a/
      /products/a/b/
      /products/a/b/c/


      And did not match this:



      /products/a/b/c/d/


      If you always wanted the 3 subdirectories, drop the ‘?’s to make the extras required.



      ^/products/[^/]+/[^/]+/[^/]+/$


      Hope this helps!






      share|improve this answer


























        1












        1








        1







        I don’t have a system to test this, but I think you can use the ‘?’ and parens to add the variable sub-dirs on your base match for “/products/“.



        The base regex would be “^/products/$”.
        Then to match 0 or 1 subdirectories, you could add “([^/]+/)?”
        Add three of these and I think you have the regex to match your base plus 0 to 3 subdirectories. I think that is what you were asking for.



        ^/products/([^/]+/)?([^/]+/)?([^/]+/)?$


        The equivalent RE worked in Emacs for these test lines:



        /products/
        /products/a/
        /products/a/b/
        /products/a/b/c/


        And did not match this:



        /products/a/b/c/d/


        If you always wanted the 3 subdirectories, drop the ‘?’s to make the extras required.



        ^/products/[^/]+/[^/]+/[^/]+/$


        Hope this helps!






        share|improve this answer













        I don’t have a system to test this, but I think you can use the ‘?’ and parens to add the variable sub-dirs on your base match for “/products/“.



        The base regex would be “^/products/$”.
        Then to match 0 or 1 subdirectories, you could add “([^/]+/)?”
        Add three of these and I think you have the regex to match your base plus 0 to 3 subdirectories. I think that is what you were asking for.



        ^/products/([^/]+/)?([^/]+/)?([^/]+/)?$


        The equivalent RE worked in Emacs for these test lines:



        /products/
        /products/a/
        /products/a/b/
        /products/a/b/c/


        And did not match this:



        /products/a/b/c/d/


        If you always wanted the 3 subdirectories, drop the ‘?’s to make the extras required.



        ^/products/[^/]+/[^/]+/[^/]+/$


        Hope this helps!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 8:00









        Bill StaffordBill Stafford

        263




        263
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405369%2fregex-to-match-number-of-subdirectories-in-a-url%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents