Regex - counting the number of colons in a string












2















New here so apologise if i miss out any critical information!



I've been using https://regex101.com/ to try and build some regex for the following -



I want to use some regex code to either return true if the number of colons in a text string is 3 and false if it is 4 or more, for example the following text string should return false -



Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17



but this should return a match -



Bin Not Out at:12:54:38



Alternatively I had been looking at other methods to get a relevant response such as ddDddDddD i.e. if there is a letter straight after a digit return a true value but i'm really struggling trying to get it to work.



Any help on either option would be appreciated as either would flag what I need.



Many thanks!










share|improve this question























  • Just count the colons. Using python as an example len("yourstring".split(":"))

    – Fredrik Pihl
    Nov 13 '18 at 11:30













  • What happens if you have one or two colons in the string?

    – JGNI
    Nov 13 '18 at 11:32











  • @FredrikPihl you will need a -1 on that. If you split on a string with 1 : the length is 2.

    – Mark Baijens
    Nov 13 '18 at 11:32











  • @MarkBaijens - True.

    – Fredrik Pihl
    Nov 13 '18 at 11:33
















2















New here so apologise if i miss out any critical information!



I've been using https://regex101.com/ to try and build some regex for the following -



I want to use some regex code to either return true if the number of colons in a text string is 3 and false if it is 4 or more, for example the following text string should return false -



Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17



but this should return a match -



Bin Not Out at:12:54:38



Alternatively I had been looking at other methods to get a relevant response such as ddDddDddD i.e. if there is a letter straight after a digit return a true value but i'm really struggling trying to get it to work.



Any help on either option would be appreciated as either would flag what I need.



Many thanks!










share|improve this question























  • Just count the colons. Using python as an example len("yourstring".split(":"))

    – Fredrik Pihl
    Nov 13 '18 at 11:30













  • What happens if you have one or two colons in the string?

    – JGNI
    Nov 13 '18 at 11:32











  • @FredrikPihl you will need a -1 on that. If you split on a string with 1 : the length is 2.

    – Mark Baijens
    Nov 13 '18 at 11:32











  • @MarkBaijens - True.

    – Fredrik Pihl
    Nov 13 '18 at 11:33














2












2








2








New here so apologise if i miss out any critical information!



I've been using https://regex101.com/ to try and build some regex for the following -



I want to use some regex code to either return true if the number of colons in a text string is 3 and false if it is 4 or more, for example the following text string should return false -



Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17



but this should return a match -



Bin Not Out at:12:54:38



Alternatively I had been looking at other methods to get a relevant response such as ddDddDddD i.e. if there is a letter straight after a digit return a true value but i'm really struggling trying to get it to work.



Any help on either option would be appreciated as either would flag what I need.



Many thanks!










share|improve this question














New here so apologise if i miss out any critical information!



I've been using https://regex101.com/ to try and build some regex for the following -



I want to use some regex code to either return true if the number of colons in a text string is 3 and false if it is 4 or more, for example the following text string should return false -



Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17



but this should return a match -



Bin Not Out at:12:54:38



Alternatively I had been looking at other methods to get a relevant response such as ddDddDddD i.e. if there is a letter straight after a digit return a true value but i'm really struggling trying to get it to work.



Any help on either option would be appreciated as either would flag what I need.



Many thanks!







regex count expression






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 11:26









JamesJames

132




132













  • Just count the colons. Using python as an example len("yourstring".split(":"))

    – Fredrik Pihl
    Nov 13 '18 at 11:30













  • What happens if you have one or two colons in the string?

    – JGNI
    Nov 13 '18 at 11:32











  • @FredrikPihl you will need a -1 on that. If you split on a string with 1 : the length is 2.

    – Mark Baijens
    Nov 13 '18 at 11:32











  • @MarkBaijens - True.

    – Fredrik Pihl
    Nov 13 '18 at 11:33



















  • Just count the colons. Using python as an example len("yourstring".split(":"))

    – Fredrik Pihl
    Nov 13 '18 at 11:30













  • What happens if you have one or two colons in the string?

    – JGNI
    Nov 13 '18 at 11:32











  • @FredrikPihl you will need a -1 on that. If you split on a string with 1 : the length is 2.

    – Mark Baijens
    Nov 13 '18 at 11:32











  • @MarkBaijens - True.

    – Fredrik Pihl
    Nov 13 '18 at 11:33

















Just count the colons. Using python as an example len("yourstring".split(":"))

– Fredrik Pihl
Nov 13 '18 at 11:30







Just count the colons. Using python as an example len("yourstring".split(":"))

– Fredrik Pihl
Nov 13 '18 at 11:30















What happens if you have one or two colons in the string?

– JGNI
Nov 13 '18 at 11:32





What happens if you have one or two colons in the string?

– JGNI
Nov 13 '18 at 11:32













@FredrikPihl you will need a -1 on that. If you split on a string with 1 : the length is 2.

– Mark Baijens
Nov 13 '18 at 11:32





@FredrikPihl you will need a -1 on that. If you split on a string with 1 : the length is 2.

– Mark Baijens
Nov 13 '18 at 11:32













@MarkBaijens - True.

– Fredrik Pihl
Nov 13 '18 at 11:33





@MarkBaijens - True.

– Fredrik Pihl
Nov 13 '18 at 11:33












3 Answers
3






active

oldest

votes


















0














To match the format in your example data containing 3 times a colon, you might use:



^[^rn:]*:d{2}:d{2}:d{2}[^rn:]*$


Regex demo



That would match





  • ^ Assert start of the string


  • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


  • (?::d{2}){3} Match a colon followed by 2 digits and repeat that 3 times


  • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


  • $ Assert the end of the line


If the values to want to match are in 24h format, you might use this regex



^[^rn:]*:(?:2[0-3]|[01]?[0-9]):(?:[0-5]?[0-9]):(?:[0-5]?[0-9])[^rn:]*$


Regex demo






share|improve this answer





















  • 1





    That is great, just what I was after, thank you very much for your help.

    – James
    Nov 13 '18 at 16:16



















0














A simple regex that matches strings containing 3 and only 3 colon:



^([^:]*:){3}[^:]*$


DEMO






share|improve this answer































    -1














    Using Perl one liner



    > cat colon.dat
    Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17
    Bin Not Out at:12:54:38
    > perl -ne ' { while(m/(:)/g){$x++} print $x==3 ? "truen":"falsen" ; $x=0 } ' colon.dat
    false
    true
    >


    or more elegantly



    > perl -ne ' { $x++ while(m/(:)/g) ; print $x==3 ? "truen":"falsen"  ; $x=0 } ' colon.dat
    false
    true
    >





    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%2f53280022%2fregex-counting-the-number-of-colons-in-a-string%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














      To match the format in your example data containing 3 times a colon, you might use:



      ^[^rn:]*:d{2}:d{2}:d{2}[^rn:]*$


      Regex demo



      That would match





      • ^ Assert start of the string


      • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


      • (?::d{2}){3} Match a colon followed by 2 digits and repeat that 3 times


      • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


      • $ Assert the end of the line


      If the values to want to match are in 24h format, you might use this regex



      ^[^rn:]*:(?:2[0-3]|[01]?[0-9]):(?:[0-5]?[0-9]):(?:[0-5]?[0-9])[^rn:]*$


      Regex demo






      share|improve this answer





















      • 1





        That is great, just what I was after, thank you very much for your help.

        – James
        Nov 13 '18 at 16:16
















      0














      To match the format in your example data containing 3 times a colon, you might use:



      ^[^rn:]*:d{2}:d{2}:d{2}[^rn:]*$


      Regex demo



      That would match





      • ^ Assert start of the string


      • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


      • (?::d{2}){3} Match a colon followed by 2 digits and repeat that 3 times


      • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


      • $ Assert the end of the line


      If the values to want to match are in 24h format, you might use this regex



      ^[^rn:]*:(?:2[0-3]|[01]?[0-9]):(?:[0-5]?[0-9]):(?:[0-5]?[0-9])[^rn:]*$


      Regex demo






      share|improve this answer





















      • 1





        That is great, just what I was after, thank you very much for your help.

        – James
        Nov 13 '18 at 16:16














      0












      0








      0







      To match the format in your example data containing 3 times a colon, you might use:



      ^[^rn:]*:d{2}:d{2}:d{2}[^rn:]*$


      Regex demo



      That would match





      • ^ Assert start of the string


      • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


      • (?::d{2}){3} Match a colon followed by 2 digits and repeat that 3 times


      • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


      • $ Assert the end of the line


      If the values to want to match are in 24h format, you might use this regex



      ^[^rn:]*:(?:2[0-3]|[01]?[0-9]):(?:[0-5]?[0-9]):(?:[0-5]?[0-9])[^rn:]*$


      Regex demo






      share|improve this answer















      To match the format in your example data containing 3 times a colon, you might use:



      ^[^rn:]*:d{2}:d{2}:d{2}[^rn:]*$


      Regex demo



      That would match





      • ^ Assert start of the string


      • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


      • (?::d{2}){3} Match a colon followed by 2 digits and repeat that 3 times


      • [^rn:]* Negated character class to match not 0+ times a carriage return, newline or a colon


      • $ Assert the end of the line


      If the values to want to match are in 24h format, you might use this regex



      ^[^rn:]*:(?:2[0-3]|[01]?[0-9]):(?:[0-5]?[0-9]):(?:[0-5]?[0-9])[^rn:]*$


      Regex demo







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 13 '18 at 16:17

























      answered Nov 13 '18 at 14:57









      The fourth birdThe fourth bird

      21k71326




      21k71326








      • 1





        That is great, just what I was after, thank you very much for your help.

        – James
        Nov 13 '18 at 16:16














      • 1





        That is great, just what I was after, thank you very much for your help.

        – James
        Nov 13 '18 at 16:16








      1




      1





      That is great, just what I was after, thank you very much for your help.

      – James
      Nov 13 '18 at 16:16





      That is great, just what I was after, thank you very much for your help.

      – James
      Nov 13 '18 at 16:16













      0














      A simple regex that matches strings containing 3 and only 3 colon:



      ^([^:]*:){3}[^:]*$


      DEMO






      share|improve this answer




























        0














        A simple regex that matches strings containing 3 and only 3 colon:



        ^([^:]*:){3}[^:]*$


        DEMO






        share|improve this answer


























          0












          0








          0







          A simple regex that matches strings containing 3 and only 3 colon:



          ^([^:]*:){3}[^:]*$


          DEMO






          share|improve this answer













          A simple regex that matches strings containing 3 and only 3 colon:



          ^([^:]*:){3}[^:]*$


          DEMO







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 12:39









          TotoToto

          65.1k175698




          65.1k175698























              -1














              Using Perl one liner



              > cat colon.dat
              Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17
              Bin Not Out at:12:54:38
              > perl -ne ' { while(m/(:)/g){$x++} print $x==3 ? "truen":"falsen" ; $x=0 } ' colon.dat
              false
              true
              >


              or more elegantly



              > perl -ne ' { $x++ while(m/(:)/g) ; print $x==3 ? "truen":"falsen"  ; $x=0 } ' colon.dat
              false
              true
              >





              share|improve this answer




























                -1














                Using Perl one liner



                > cat colon.dat
                Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17
                Bin Not Out at:12:54:38
                > perl -ne ' { while(m/(:)/g){$x++} print $x==3 ? "truen":"falsen" ; $x=0 } ' colon.dat
                false
                true
                >


                or more elegantly



                > perl -ne ' { $x++ while(m/(:)/g) ; print $x==3 ? "truen":"falsen"  ; $x=0 } ' colon.dat
                false
                true
                >





                share|improve this answer


























                  -1












                  -1








                  -1







                  Using Perl one liner



                  > cat colon.dat
                  Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17
                  Bin Not Out at:12:54:38
                  > perl -ne ' { while(m/(:)/g){$x++} print $x==3 ? "truen":"falsen" ; $x=0 } ' colon.dat
                  false
                  true
                  >


                  or more elegantly



                  > perl -ne ' { $x++ while(m/(:)/g) ; print $x==3 ? "truen":"falsen"  ; $x=0 } ' colon.dat
                  false
                  true
                  >





                  share|improve this answer













                  Using Perl one liner



                  > cat colon.dat
                  Bin Not Out at:12:54:38Wrong Colour Bin at:12:43:17
                  Bin Not Out at:12:54:38
                  > perl -ne ' { while(m/(:)/g){$x++} print $x==3 ? "truen":"falsen" ; $x=0 } ' colon.dat
                  false
                  true
                  >


                  or more elegantly



                  > perl -ne ' { $x++ while(m/(:)/g) ; print $x==3 ? "truen":"falsen"  ; $x=0 } ' colon.dat
                  false
                  true
                  >






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 11:33









                  stack0114106stack0114106

                  2,4161417




                  2,4161417






























                      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%2f53280022%2fregex-counting-the-number-of-colons-in-a-string%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

                      Xamarin.iOS Cant Deploy on Iphone

                      Glorious Revolution

                      Dulmage-Mendelsohn matrix decomposition in Python