Regex Match a string that is indirectly bound by 2 strings












0















Here is my abstracted test string:



test test test



blue



test test test



red 23



test test test



grey



test test test



blue



test test test



red 99



test test test



grey - white



test test test



I am trying to match the text between the second "blue" and "grey - white"



Basically I'm parsing some code, and need 2 rules:



1.) Find red if between "blue" and "grey"



2.) Find red if between "blue" and "grey - white" * I can't be sure of the order of the stanzas



The first one isn't too hard: Link



But I can't figure out the second rule. Everything I tried...
like (?s)(blue)(.*?)(grey(?!s+test))
... still matches the first "blue", instead of skipping over it



Is there away to turn (.*?) into 'but only if does not contain "blue" '



Anyone know a trick I do not?










share|improve this question



























    0















    Here is my abstracted test string:



    test test test



    blue



    test test test



    red 23



    test test test



    grey



    test test test



    blue



    test test test



    red 99



    test test test



    grey - white



    test test test



    I am trying to match the text between the second "blue" and "grey - white"



    Basically I'm parsing some code, and need 2 rules:



    1.) Find red if between "blue" and "grey"



    2.) Find red if between "blue" and "grey - white" * I can't be sure of the order of the stanzas



    The first one isn't too hard: Link



    But I can't figure out the second rule. Everything I tried...
    like (?s)(blue)(.*?)(grey(?!s+test))
    ... still matches the first "blue", instead of skipping over it



    Is there away to turn (.*?) into 'but only if does not contain "blue" '



    Anyone know a trick I do not?










    share|improve this question

























      0












      0








      0








      Here is my abstracted test string:



      test test test



      blue



      test test test



      red 23



      test test test



      grey



      test test test



      blue



      test test test



      red 99



      test test test



      grey - white



      test test test



      I am trying to match the text between the second "blue" and "grey - white"



      Basically I'm parsing some code, and need 2 rules:



      1.) Find red if between "blue" and "grey"



      2.) Find red if between "blue" and "grey - white" * I can't be sure of the order of the stanzas



      The first one isn't too hard: Link



      But I can't figure out the second rule. Everything I tried...
      like (?s)(blue)(.*?)(grey(?!s+test))
      ... still matches the first "blue", instead of skipping over it



      Is there away to turn (.*?) into 'but only if does not contain "blue" '



      Anyone know a trick I do not?










      share|improve this question














      Here is my abstracted test string:



      test test test



      blue



      test test test



      red 23



      test test test



      grey



      test test test



      blue



      test test test



      red 99



      test test test



      grey - white



      test test test



      I am trying to match the text between the second "blue" and "grey - white"



      Basically I'm parsing some code, and need 2 rules:



      1.) Find red if between "blue" and "grey"



      2.) Find red if between "blue" and "grey - white" * I can't be sure of the order of the stanzas



      The first one isn't too hard: Link



      But I can't figure out the second rule. Everything I tried...
      like (?s)(blue)(.*?)(grey(?!s+test))
      ... still matches the first "blue", instead of skipping over it



      Is there away to turn (.*?) into 'but only if does not contain "blue" '



      Anyone know a trick I do not?







      regex regex-lookarounds






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 16:31









      NorsakNorsak

      1




      1
























          3 Answers
          3






          active

          oldest

          votes


















          0














          I believe you are looking for this based on your 2 rules although they don't match your regex. regex101



          (blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)


          Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.






          share|improve this answer































            0














            (?s)(blue).*?(red).*?(grey(?: - blue)?)


            The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)



            .



            Let me re-write the question to clarify the objective:



            I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):



            1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)



            2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)






            share|improve this answer


























            • I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.

              – The fourth bird
              Jan 3 at 7:50



















            0














            This regex (?s)(blue)(.*?)(grey(?!s+test)) will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test)) which does match in the middle and fails the negative lookahead.



            What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"



            (?s)(blue).*(red d+).*?(grey - white)


            See the Regex demo






            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%2f54009870%2fregex-match-a-string-that-is-indirectly-bound-by-2-strings%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              I believe you are looking for this based on your 2 rules although they don't match your regex. regex101



              (blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)


              Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.






              share|improve this answer




























                0














                I believe you are looking for this based on your 2 rules although they don't match your regex. regex101



                (blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)


                Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.






                share|improve this answer


























                  0












                  0








                  0







                  I believe you are looking for this based on your 2 rules although they don't match your regex. regex101



                  (blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)


                  Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.






                  share|improve this answer













                  I believe you are looking for this based on your 2 rules although they don't match your regex. regex101



                  (blue).*?[^ ](red) .*?[^ ](grey( | - blue)?)


                  Match 'blue' followed by a non greedy match of anything until the next 'red' (where red is proceeded by the start of a line or a space, and followed by a space) followed by a non-greedy match of anything until followed by 'grey' (where grey is proceeded by the start of a line or a space) which is itself optionally followed by a space or ' - blue'. Ensuring the match word is proceeded by a space rules out false matches where 'red' is part of another word.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 2 at 18:25









                  Gary_WGary_W

                  6,99811129




                  6,99811129

























                      0














                      (?s)(blue).*?(red).*?(grey(?: - blue)?)


                      The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)



                      .



                      Let me re-write the question to clarify the objective:



                      I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):



                      1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)



                      2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)






                      share|improve this answer


























                      • I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.

                        – The fourth bird
                        Jan 3 at 7:50
















                      0














                      (?s)(blue).*?(red).*?(grey(?: - blue)?)


                      The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)



                      .



                      Let me re-write the question to clarify the objective:



                      I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):



                      1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)



                      2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)






                      share|improve this answer


























                      • I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.

                        – The fourth bird
                        Jan 3 at 7:50














                      0












                      0








                      0







                      (?s)(blue).*?(red).*?(grey(?: - blue)?)


                      The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)



                      .



                      Let me re-write the question to clarify the objective:



                      I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):



                      1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)



                      2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)






                      share|improve this answer















                      (?s)(blue).*?(red).*?(grey(?: - blue)?)


                      The above regex matches BOTH reds... but I need to be able to tell which is which (I can't rely on their order)



                      .



                      Let me re-write the question to clarify the objective:



                      I need two regex, I already found an answer for 1.), but am looking for an answer for 2.):



                      1.) Find "red XX" if between "blue" and "grey" (match contains only red 23)



                      2.) Find "red XX" if between "blue" and "grey - white" (match contains only red 99)







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 2 at 23:31

























                      answered Jan 2 at 22:09









                      NorsakNorsak

                      44




                      44













                      • I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.

                        – The fourth bird
                        Jan 3 at 7:50



















                      • I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.

                        – The fourth bird
                        Jan 3 at 7:50

















                      I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.

                      – The fourth bird
                      Jan 3 at 7:50





                      I have update my answer. Instead of re-writing your question as an answer you could add it to the question instead.

                      – The fourth bird
                      Jan 3 at 7:50











                      0














                      This regex (?s)(blue)(.*?)(grey(?!s+test)) will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test)) which does match in the middle and fails the negative lookahead.



                      What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"



                      (?s)(blue).*(red d+).*?(grey - white)


                      See the Regex demo






                      share|improve this answer






























                        0














                        This regex (?s)(blue)(.*?)(grey(?!s+test)) will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test)) which does match in the middle and fails the negative lookahead.



                        What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"



                        (?s)(blue).*(red d+).*?(grey - white)


                        See the Regex demo






                        share|improve this answer




























                          0












                          0








                          0







                          This regex (?s)(blue)(.*?)(grey(?!s+test)) will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test)) which does match in the middle and fails the negative lookahead.



                          What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"



                          (?s)(blue).*(red d+).*?(grey - white)


                          See the Regex demo






                          share|improve this answer















                          This regex (?s)(blue)(.*?)(grey(?!s+test)) will match the whole part between the first blue and the last grey because of the negative lookahead (grey(?!s+test)) which does match in the middle and fails the negative lookahead.



                          What you might do is capture blue and then use a greedy match to match the last "red" followed by 1+ digits. Then use a non greedy pattern to match until the first "grey - white"



                          (?s)(blue).*(red d+).*?(grey - white)


                          See the Regex demo







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 3 at 7:48

























                          answered Jan 2 at 19:20









                          The fourth birdThe fourth bird

                          24.3k81629




                          24.3k81629






























                              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%2f54009870%2fregex-match-a-string-that-is-indirectly-bound-by-2-strings%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

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              Npm cannot find a required file even through it is in the searched directory