Regular expression for a string containing one word but not another











up vote
69
down vote

favorite
17












I'm setting up some goals in Google Analytics and could use a little regex help.



Lets say I have 4 URLs



http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1


I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm



I know that to find a string that does NOT contain another string I can use this expression:



(^((?!details.cfm).)*$)


But, I'm not sure how to add in the selector=size portion.



Any help would be greatly appreciated!










share|improve this question


























    up vote
    69
    down vote

    favorite
    17












    I'm setting up some goals in Google Analytics and could use a little regex help.



    Lets say I have 4 URLs



    http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
    http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
    http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
    http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1


    I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm



    I know that to find a string that does NOT contain another string I can use this expression:



    (^((?!details.cfm).)*$)


    But, I'm not sure how to add in the selector=size portion.



    Any help would be greatly appreciated!










    share|improve this question
























      up vote
      69
      down vote

      favorite
      17









      up vote
      69
      down vote

      favorite
      17






      17





      I'm setting up some goals in Google Analytics and could use a little regex help.



      Lets say I have 4 URLs



      http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
      http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
      http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
      http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1


      I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm



      I know that to find a string that does NOT contain another string I can use this expression:



      (^((?!details.cfm).)*$)


      But, I'm not sure how to add in the selector=size portion.



      Any help would be greatly appreciated!










      share|improve this question













      I'm setting up some goals in Google Analytics and could use a little regex help.



      Lets say I have 4 URLs



      http://www.anydotcom.com/test/search.cfm?metric=blah&selector=size&value=1
      http://www.anydotcom.com/test/search.cfm?metric=blah2&selector=style&value=1
      http://www.anydotcom.com/test/search.cfm?metric=blah3&selector=size&value=1
      http://www.anydotcom.com/test/details.cfm?metric=blah&selector=size&value=1


      I want to create an expression that will identify any URL that contains the string selector=size but does NOT contain details.cfm



      I know that to find a string that does NOT contain another string I can use this expression:



      (^((?!details.cfm).)*$)


      But, I'm not sure how to add in the selector=size portion.



      Any help would be greatly appreciated!







      regex google-analytics regex-negation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 1 '10 at 20:21









      Chris Stahl

      5241615




      5241615
























          5 Answers
          5






          active

          oldest

          votes

















          up vote
          101
          down vote



          accepted










          This should do it:



          ^(?!.*details.cfm).*selector=size.*$


          ^.*selector=size.*$ should be clear enough. The first bit, (?!.*details.cfm) is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).






          share|improve this answer

















          • 4




            FYI, check out regexr.com for a nice way to test these expressions out.
            – Joshua Pinter
            Apr 8 '14 at 14:23










          • Brilliant, this helped. Good explanation
            – user219628
            Dec 21 '15 at 18:02










          • Always forget about negative lookahead and it's so useful
            – Alexei Blue
            Feb 20 at 15:35


















          up vote
          5
          down vote













          regex could be (perl syntax):



          `/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`





          share|improve this answer






























            up vote
            1
            down vote













            ^(?=.*selector=size)(?:(?!details.cfm).)+$


            If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:



            ^[^?]*+(?<!details.cfm).*?selector=size.*$





            share|improve this answer























            • This assumes selector=size is always before details.cfm, which isn't the case in the last url.
              – Kobi
              Jun 1 '10 at 20:34










            • Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
              – Kobi
              Jun 1 '10 at 20:47










            • @Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
              – Tomalak
              Jun 1 '10 at 20:48




















            up vote
            0
            down vote













            I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).



            My original command:



            tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '


            Now becomes (with "-P" perl switch):



            tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'





            share|improve this answer






























              up vote
              -4
              down vote













              Simple way to do this is to specify 0 instances of the string by doing the following



              (string_to_exclude){0}





              share|improve this answer



















              • 2




                This does not work.
                – Austin Henley
                Oct 10 '12 at 20:06










              • this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
                – Zoey Hewll
                Apr 4 '17 at 5:01











              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',
              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%2f2953039%2fregular-expression-for-a-string-containing-one-word-but-not-another%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              5 Answers
              5






              active

              oldest

              votes








              5 Answers
              5






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              101
              down vote



              accepted










              This should do it:



              ^(?!.*details.cfm).*selector=size.*$


              ^.*selector=size.*$ should be clear enough. The first bit, (?!.*details.cfm) is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).






              share|improve this answer

















              • 4




                FYI, check out regexr.com for a nice way to test these expressions out.
                – Joshua Pinter
                Apr 8 '14 at 14:23










              • Brilliant, this helped. Good explanation
                – user219628
                Dec 21 '15 at 18:02










              • Always forget about negative lookahead and it's so useful
                – Alexei Blue
                Feb 20 at 15:35















              up vote
              101
              down vote



              accepted










              This should do it:



              ^(?!.*details.cfm).*selector=size.*$


              ^.*selector=size.*$ should be clear enough. The first bit, (?!.*details.cfm) is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).






              share|improve this answer

















              • 4




                FYI, check out regexr.com for a nice way to test these expressions out.
                – Joshua Pinter
                Apr 8 '14 at 14:23










              • Brilliant, this helped. Good explanation
                – user219628
                Dec 21 '15 at 18:02










              • Always forget about negative lookahead and it's so useful
                – Alexei Blue
                Feb 20 at 15:35













              up vote
              101
              down vote



              accepted







              up vote
              101
              down vote



              accepted






              This should do it:



              ^(?!.*details.cfm).*selector=size.*$


              ^.*selector=size.*$ should be clear enough. The first bit, (?!.*details.cfm) is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).






              share|improve this answer












              This should do it:



              ^(?!.*details.cfm).*selector=size.*$


              ^.*selector=size.*$ should be clear enough. The first bit, (?!.*details.cfm) is a negative look-ahead: before matching the string it checks the string does not contain "details.cfm" (with any number of characters before it).







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jun 1 '10 at 20:26









              Kobi

              106k33217263




              106k33217263








              • 4




                FYI, check out regexr.com for a nice way to test these expressions out.
                – Joshua Pinter
                Apr 8 '14 at 14:23










              • Brilliant, this helped. Good explanation
                – user219628
                Dec 21 '15 at 18:02










              • Always forget about negative lookahead and it's so useful
                – Alexei Blue
                Feb 20 at 15:35














              • 4




                FYI, check out regexr.com for a nice way to test these expressions out.
                – Joshua Pinter
                Apr 8 '14 at 14:23










              • Brilliant, this helped. Good explanation
                – user219628
                Dec 21 '15 at 18:02










              • Always forget about negative lookahead and it's so useful
                – Alexei Blue
                Feb 20 at 15:35








              4




              4




              FYI, check out regexr.com for a nice way to test these expressions out.
              – Joshua Pinter
              Apr 8 '14 at 14:23




              FYI, check out regexr.com for a nice way to test these expressions out.
              – Joshua Pinter
              Apr 8 '14 at 14:23












              Brilliant, this helped. Good explanation
              – user219628
              Dec 21 '15 at 18:02




              Brilliant, this helped. Good explanation
              – user219628
              Dec 21 '15 at 18:02












              Always forget about negative lookahead and it's so useful
              – Alexei Blue
              Feb 20 at 15:35




              Always forget about negative lookahead and it's so useful
              – Alexei Blue
              Feb 20 at 15:35












              up vote
              5
              down vote













              regex could be (perl syntax):



              `/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`





              share|improve this answer



























                up vote
                5
                down vote













                regex could be (perl syntax):



                `/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`





                share|improve this answer

























                  up vote
                  5
                  down vote










                  up vote
                  5
                  down vote









                  regex could be (perl syntax):



                  `/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`





                  share|improve this answer














                  regex could be (perl syntax):



                  `/^[(^(?!.*details.cfm).*selector=size.*)|(selector=size.*^(?!.*details.cfm).*)]$/`






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jun 1 '10 at 20:37

























                  answered Jun 1 '10 at 20:35









                  djipko

                  913




                  913






















                      up vote
                      1
                      down vote













                      ^(?=.*selector=size)(?:(?!details.cfm).)+$


                      If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:



                      ^[^?]*+(?<!details.cfm).*?selector=size.*$





                      share|improve this answer























                      • This assumes selector=size is always before details.cfm, which isn't the case in the last url.
                        – Kobi
                        Jun 1 '10 at 20:34










                      • Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
                        – Kobi
                        Jun 1 '10 at 20:47










                      • @Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
                        – Tomalak
                        Jun 1 '10 at 20:48

















                      up vote
                      1
                      down vote













                      ^(?=.*selector=size)(?:(?!details.cfm).)+$


                      If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:



                      ^[^?]*+(?<!details.cfm).*?selector=size.*$





                      share|improve this answer























                      • This assumes selector=size is always before details.cfm, which isn't the case in the last url.
                        – Kobi
                        Jun 1 '10 at 20:34










                      • Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
                        – Kobi
                        Jun 1 '10 at 20:47










                      • @Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
                        – Tomalak
                        Jun 1 '10 at 20:48















                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      ^(?=.*selector=size)(?:(?!details.cfm).)+$


                      If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:



                      ^[^?]*+(?<!details.cfm).*?selector=size.*$





                      share|improve this answer














                      ^(?=.*selector=size)(?:(?!details.cfm).)+$


                      If your regex engine supported posessive quantifiers (though I suspect Google Analytics does not), then I guess this will perform better for large input sets:



                      ^[^?]*+(?<!details.cfm).*?selector=size.*$






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 1 '10 at 20:35

























                      answered Jun 1 '10 at 20:27









                      Tomalak

                      254k51422538




                      254k51422538












                      • This assumes selector=size is always before details.cfm, which isn't the case in the last url.
                        – Kobi
                        Jun 1 '10 at 20:34










                      • Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
                        – Kobi
                        Jun 1 '10 at 20:47










                      • @Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
                        – Tomalak
                        Jun 1 '10 at 20:48




















                      • This assumes selector=size is always before details.cfm, which isn't the case in the last url.
                        – Kobi
                        Jun 1 '10 at 20:34










                      • Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
                        – Kobi
                        Jun 1 '10 at 20:47










                      • @Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
                        – Tomalak
                        Jun 1 '10 at 20:48


















                      This assumes selector=size is always before details.cfm, which isn't the case in the last url.
                      – Kobi
                      Jun 1 '10 at 20:34




                      This assumes selector=size is always before details.cfm, which isn't the case in the last url.
                      – Kobi
                      Jun 1 '10 at 20:34












                      Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
                      – Kobi
                      Jun 1 '10 at 20:47




                      Just to clear this up, it wasn't me. I can't see why someone would down-vote two answers here, they are both correct.
                      – Kobi
                      Jun 1 '10 at 20:47












                      @Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
                      – Tomalak
                      Jun 1 '10 at 20:48






                      @Kobi: This should have been a look-ahead, corrected. Oh and by the way, I did not suspect it was your down-vote.
                      – Tomalak
                      Jun 1 '10 at 20:48












                      up vote
                      0
                      down vote













                      I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).



                      My original command:



                      tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '


                      Now becomes (with "-P" perl switch):



                      tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'





                      share|improve this answer



























                        up vote
                        0
                        down vote













                        I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).



                        My original command:



                        tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '


                        Now becomes (with "-P" perl switch):



                        tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'





                        share|improve this answer

























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).



                          My original command:



                          tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '


                          Now becomes (with "-P" perl switch):



                          tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'





                          share|improve this answer














                          I was looking for a way to avoid --line-buffered on a tail in a similar situation as the OP and Kobi's solution works great for me. In my case excluding lines with either "bot" or "spider" while including ' / ' (for my root document).



                          My original command:



                          tail -f mylogfile | grep --line-buffered -v 'bot|spider' | grep ' / '


                          Now becomes (with "-P" perl switch):



                          tail -f mylogfile | grep -P '^(?!.*(bot|spider)).*s/s.*$'






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jun 16 '16 at 11:33

























                          answered Jun 16 '16 at 11:11









                          roon

                          11




                          11






















                              up vote
                              -4
                              down vote













                              Simple way to do this is to specify 0 instances of the string by doing the following



                              (string_to_exclude){0}





                              share|improve this answer



















                              • 2




                                This does not work.
                                – Austin Henley
                                Oct 10 '12 at 20:06










                              • this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
                                – Zoey Hewll
                                Apr 4 '17 at 5:01















                              up vote
                              -4
                              down vote













                              Simple way to do this is to specify 0 instances of the string by doing the following



                              (string_to_exclude){0}





                              share|improve this answer



















                              • 2




                                This does not work.
                                – Austin Henley
                                Oct 10 '12 at 20:06










                              • this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
                                – Zoey Hewll
                                Apr 4 '17 at 5:01













                              up vote
                              -4
                              down vote










                              up vote
                              -4
                              down vote









                              Simple way to do this is to specify 0 instances of the string by doing the following



                              (string_to_exclude){0}





                              share|improve this answer














                              Simple way to do this is to specify 0 instances of the string by doing the following



                              (string_to_exclude){0}






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jul 27 '12 at 11:53









                              Taryn

                              187k45284348




                              187k45284348










                              answered Jul 27 '12 at 1:36









                              Trace Johnson

                              3




                              3








                              • 2




                                This does not work.
                                – Austin Henley
                                Oct 10 '12 at 20:06










                              • this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
                                – Zoey Hewll
                                Apr 4 '17 at 5:01














                              • 2




                                This does not work.
                                – Austin Henley
                                Oct 10 '12 at 20:06










                              • this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
                                – Zoey Hewll
                                Apr 4 '17 at 5:01








                              2




                              2




                              This does not work.
                              – Austin Henley
                              Oct 10 '12 at 20:06




                              This does not work.
                              – Austin Henley
                              Oct 10 '12 at 20:06












                              this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
                              – Zoey Hewll
                              Apr 4 '17 at 5:01




                              this simply evaluates to the empty string; it does not ensure that the substring does not occur, but that the empty string does occur, which it always does
                              – Zoey Hewll
                              Apr 4 '17 at 5:01


















                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2953039%2fregular-expression-for-a-string-containing-one-word-but-not-another%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

                              android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                              SQL update select statement

                              'app-layout' is not a known element: how to share Component with different Modules