PCRE Regex Match /x… but not /y/x












2















When configuring redirections, it's common to run into multiple pages that include some of the same path strings. We've ran into this instance multiple times where we need to redirect:



https://example.com/x...


But not:



https://example.com/y/x...


To match the /x... we use PCRE regex of:



/x.*


We've been struggling to get the exclude to match correctly; we apologize in advance as our regex is a bit weak, here's our pseudo code:



Match all /x... except /y/x...


Here is what we thought that looked like:



^/(?!y/).x.*


In our mind that reads:



Any query starting with /x..., except starting with /y/x...


Thank you in advance, and please feel free to suggest better formatting, we are not stack overflow pros.










share|improve this question





























    2















    When configuring redirections, it's common to run into multiple pages that include some of the same path strings. We've ran into this instance multiple times where we need to redirect:



    https://example.com/x...


    But not:



    https://example.com/y/x...


    To match the /x... we use PCRE regex of:



    /x.*


    We've been struggling to get the exclude to match correctly; we apologize in advance as our regex is a bit weak, here's our pseudo code:



    Match all /x... except /y/x...


    Here is what we thought that looked like:



    ^/(?!y/).x.*


    In our mind that reads:



    Any query starting with /x..., except starting with /y/x...


    Thank you in advance, and please feel free to suggest better formatting, we are not stack overflow pros.










    share|improve this question



























      2












      2








      2








      When configuring redirections, it's common to run into multiple pages that include some of the same path strings. We've ran into this instance multiple times where we need to redirect:



      https://example.com/x...


      But not:



      https://example.com/y/x...


      To match the /x... we use PCRE regex of:



      /x.*


      We've been struggling to get the exclude to match correctly; we apologize in advance as our regex is a bit weak, here's our pseudo code:



      Match all /x... except /y/x...


      Here is what we thought that looked like:



      ^/(?!y/).x.*


      In our mind that reads:



      Any query starting with /x..., except starting with /y/x...


      Thank you in advance, and please feel free to suggest better formatting, we are not stack overflow pros.










      share|improve this question
















      When configuring redirections, it's common to run into multiple pages that include some of the same path strings. We've ran into this instance multiple times where we need to redirect:



      https://example.com/x...


      But not:



      https://example.com/y/x...


      To match the /x... we use PCRE regex of:



      /x.*


      We've been struggling to get the exclude to match correctly; we apologize in advance as our regex is a bit weak, here's our pseudo code:



      Match all /x... except /y/x...


      Here is what we thought that looked like:



      ^/(?!y/).x.*


      In our mind that reads:



      Any query starting with /x..., except starting with /y/x...


      Thank you in advance, and please feel free to suggest better formatting, we are not stack overflow pros.







      regex pcre






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 28 '18 at 15:32







      beta208

















      asked Nov 28 '18 at 15:24









      beta208beta208

      191315




      191315
























          2 Answers
          2






          active

          oldest

          votes


















          1














          Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/. If that is true, then match any character followed by x and 0+ character. That will match for example //x///



          Without taking matching the url part into account, one way could be to use a negative lookahead (?! to check if what is on the right side does not contain /y/x and then match any character:



          ^(?!.*/y/x).+



          Regex demo






          share|improve this answer


























          • Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*

            – beta208
            Nov 28 '18 at 15:35













          • You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...

            – beta208
            Nov 28 '18 at 15:38













          • I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.

            – Poul Bak
            Nov 28 '18 at 15:40






          • 1





            @beta208 That would work ^(?!.*/y/x).*/x.* if there should be /x in the string. It will match until the last occurrence of /x and matches any character 0+ times after it.

            – The fourth bird
            Nov 28 '18 at 15:50






          • 1





            Thank you that does make sense.

            – beta208
            Nov 28 '18 at 15:53



















          1














          You may use a negative lookbehind assertion:



          ~(?<!/y)/x~


          RegEx Demo



          (?<!/y) is a negative lookbehind assertnion that will fail the match if /y appears before matching /x.






          share|improve this answer
























          • I do see that this also matches and is shorter. I imagine this is the ideal solution?

            – beta208
            Nov 28 '18 at 15:47






          • 1





            Yes your requirement needed a lookbehind, moreover this one is more efficient than ^(?!.*/y/x).*/x as you can see in no of steps taken in regex demo link I provided.

            – anubhava
            Nov 28 '18 at 15:50











          • With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*

            – beta208
            Nov 28 '18 at 16:28













          • Sorry didn't understand what you mean by that. What is your actual regex on your system?

            – anubhava
            Nov 28 '18 at 16:44











          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%2f53522819%2fpcre-regex-match-x-but-not-y-x%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/. If that is true, then match any character followed by x and 0+ character. That will match for example //x///



          Without taking matching the url part into account, one way could be to use a negative lookahead (?! to check if what is on the right side does not contain /y/x and then match any character:



          ^(?!.*/y/x).+



          Regex demo






          share|improve this answer


























          • Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*

            – beta208
            Nov 28 '18 at 15:35













          • You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...

            – beta208
            Nov 28 '18 at 15:38













          • I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.

            – Poul Bak
            Nov 28 '18 at 15:40






          • 1





            @beta208 That would work ^(?!.*/y/x).*/x.* if there should be /x in the string. It will match until the last occurrence of /x and matches any character 0+ times after it.

            – The fourth bird
            Nov 28 '18 at 15:50






          • 1





            Thank you that does make sense.

            – beta208
            Nov 28 '18 at 15:53
















          1














          Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/. If that is true, then match any character followed by x and 0+ character. That will match for example //x///



          Without taking matching the url part into account, one way could be to use a negative lookahead (?! to check if what is on the right side does not contain /y/x and then match any character:



          ^(?!.*/y/x).+



          Regex demo






          share|improve this answer


























          • Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*

            – beta208
            Nov 28 '18 at 15:35













          • You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...

            – beta208
            Nov 28 '18 at 15:38













          • I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.

            – Poul Bak
            Nov 28 '18 at 15:40






          • 1





            @beta208 That would work ^(?!.*/y/x).*/x.* if there should be /x in the string. It will match until the last occurrence of /x and matches any character 0+ times after it.

            – The fourth bird
            Nov 28 '18 at 15:50






          • 1





            Thank you that does make sense.

            – beta208
            Nov 28 '18 at 15:53














          1












          1








          1







          Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/. If that is true, then match any character followed by x and 0+ character. That will match for example //x///



          Without taking matching the url part into account, one way could be to use a negative lookahead (?! to check if what is on the right side does not contain /y/x and then match any character:



          ^(?!.*/y/x).+



          Regex demo






          share|improve this answer















          Your regex matches from the start of the string a forward slash and then uses a negative lookahead to check what follows is not y/. If that is true, then match any character followed by x and 0+ character. That will match for example //x///



          Without taking matching the url part into account, one way could be to use a negative lookahead (?! to check if what is on the right side does not contain /y/x and then match any character:



          ^(?!.*/y/x).+



          Regex demo







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 15:40

























          answered Nov 28 '18 at 15:30









          The fourth birdThe fourth bird

          24.2k81629




          24.2k81629













          • Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*

            – beta208
            Nov 28 '18 at 15:35













          • You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...

            – beta208
            Nov 28 '18 at 15:38













          • I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.

            – Poul Bak
            Nov 28 '18 at 15:40






          • 1





            @beta208 That would work ^(?!.*/y/x).*/x.* if there should be /x in the string. It will match until the last occurrence of /x and matches any character 0+ times after it.

            – The fourth bird
            Nov 28 '18 at 15:50






          • 1





            Thank you that does make sense.

            – beta208
            Nov 28 '18 at 15:53



















          • Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*

            – beta208
            Nov 28 '18 at 15:35













          • You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...

            – beta208
            Nov 28 '18 at 15:38













          • I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.

            – Poul Bak
            Nov 28 '18 at 15:40






          • 1





            @beta208 That would work ^(?!.*/y/x).*/x.* if there should be /x in the string. It will match until the last occurrence of /x and matches any character 0+ times after it.

            – The fourth bird
            Nov 28 '18 at 15:50






          • 1





            Thank you that does make sense.

            – beta208
            Nov 28 '18 at 15:53

















          Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*

          – beta208
          Nov 28 '18 at 15:35







          Thank you, this is a helpful starting place. You got exactly the half we are missing, which is exclude /y/x... But that would also match all other queries like /z/. Is that intended to append before regex to match /x/? Something like ^(?!.*/y/x).+x.*

          – beta208
          Nov 28 '18 at 15:35















          You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...

          – beta208
          Nov 28 '18 at 15:38







          You are brilliant, that worked: ^(?!.*/y/x).*/x.* Can you run me through what that reads in pseudo code, so in the future rather than copying this users can learn what they may have misunderstood? I see it as all queries that start with excluding all instances of /y/x, except when /x...

          – beta208
          Nov 28 '18 at 15:38















          I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.

          – Poul Bak
          Nov 28 '18 at 15:40





          I think you need: ^(?!.*/y/x)/x.+ to match only if it has /x in the path.

          – Poul Bak
          Nov 28 '18 at 15:40




          1




          1





          @beta208 That would work ^(?!.*/y/x).*/x.* if there should be /x in the string. It will match until the last occurrence of /x and matches any character 0+ times after it.

          – The fourth bird
          Nov 28 '18 at 15:50





          @beta208 That would work ^(?!.*/y/x).*/x.* if there should be /x in the string. It will match until the last occurrence of /x and matches any character 0+ times after it.

          – The fourth bird
          Nov 28 '18 at 15:50




          1




          1





          Thank you that does make sense.

          – beta208
          Nov 28 '18 at 15:53





          Thank you that does make sense.

          – beta208
          Nov 28 '18 at 15:53













          1














          You may use a negative lookbehind assertion:



          ~(?<!/y)/x~


          RegEx Demo



          (?<!/y) is a negative lookbehind assertnion that will fail the match if /y appears before matching /x.






          share|improve this answer
























          • I do see that this also matches and is shorter. I imagine this is the ideal solution?

            – beta208
            Nov 28 '18 at 15:47






          • 1





            Yes your requirement needed a lookbehind, moreover this one is more efficient than ^(?!.*/y/x).*/x as you can see in no of steps taken in regex demo link I provided.

            – anubhava
            Nov 28 '18 at 15:50











          • With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*

            – beta208
            Nov 28 '18 at 16:28













          • Sorry didn't understand what you mean by that. What is your actual regex on your system?

            – anubhava
            Nov 28 '18 at 16:44
















          1














          You may use a negative lookbehind assertion:



          ~(?<!/y)/x~


          RegEx Demo



          (?<!/y) is a negative lookbehind assertnion that will fail the match if /y appears before matching /x.






          share|improve this answer
























          • I do see that this also matches and is shorter. I imagine this is the ideal solution?

            – beta208
            Nov 28 '18 at 15:47






          • 1





            Yes your requirement needed a lookbehind, moreover this one is more efficient than ^(?!.*/y/x).*/x as you can see in no of steps taken in regex demo link I provided.

            – anubhava
            Nov 28 '18 at 15:50











          • With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*

            – beta208
            Nov 28 '18 at 16:28













          • Sorry didn't understand what you mean by that. What is your actual regex on your system?

            – anubhava
            Nov 28 '18 at 16:44














          1












          1








          1







          You may use a negative lookbehind assertion:



          ~(?<!/y)/x~


          RegEx Demo



          (?<!/y) is a negative lookbehind assertnion that will fail the match if /y appears before matching /x.






          share|improve this answer













          You may use a negative lookbehind assertion:



          ~(?<!/y)/x~


          RegEx Demo



          (?<!/y) is a negative lookbehind assertnion that will fail the match if /y appears before matching /x.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 28 '18 at 15:38









          anubhavaanubhava

          533k48331408




          533k48331408













          • I do see that this also matches and is shorter. I imagine this is the ideal solution?

            – beta208
            Nov 28 '18 at 15:47






          • 1





            Yes your requirement needed a lookbehind, moreover this one is more efficient than ^(?!.*/y/x).*/x as you can see in no of steps taken in regex demo link I provided.

            – anubhava
            Nov 28 '18 at 15:50











          • With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*

            – beta208
            Nov 28 '18 at 16:28













          • Sorry didn't understand what you mean by that. What is your actual regex on your system?

            – anubhava
            Nov 28 '18 at 16:44



















          • I do see that this also matches and is shorter. I imagine this is the ideal solution?

            – beta208
            Nov 28 '18 at 15:47






          • 1





            Yes your requirement needed a lookbehind, moreover this one is more efficient than ^(?!.*/y/x).*/x as you can see in no of steps taken in regex demo link I provided.

            – anubhava
            Nov 28 '18 at 15:50











          • With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*

            – beta208
            Nov 28 '18 at 16:28













          • Sorry didn't understand what you mean by that. What is your actual regex on your system?

            – anubhava
            Nov 28 '18 at 16:44

















          I do see that this also matches and is shorter. I imagine this is the ideal solution?

          – beta208
          Nov 28 '18 at 15:47





          I do see that this also matches and is shorter. I imagine this is the ideal solution?

          – beta208
          Nov 28 '18 at 15:47




          1




          1





          Yes your requirement needed a lookbehind, moreover this one is more efficient than ^(?!.*/y/x).*/x as you can see in no of steps taken in regex demo link I provided.

          – anubhava
          Nov 28 '18 at 15:50





          Yes your requirement needed a lookbehind, moreover this one is more efficient than ^(?!.*/y/x).*/x as you can see in no of steps taken in regex demo link I provided.

          – anubhava
          Nov 28 '18 at 15:50













          With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*

          – beta208
          Nov 28 '18 at 16:28







          With our system this one did not match /lunch/ but only /lunch. Would it be: ~(?<!/y)/x.*

          – beta208
          Nov 28 '18 at 16:28















          Sorry didn't understand what you mean by that. What is your actual regex on your system?

          – anubhava
          Nov 28 '18 at 16:44





          Sorry didn't understand what you mean by that. What is your actual regex on your system?

          – anubhava
          Nov 28 '18 at 16:44


















          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%2f53522819%2fpcre-regex-match-x-but-not-y-x%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

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)