Regex - counting the number of colons in a string
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
add a comment |
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
Just count the colons. Using python as an examplelen("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
add a comment |
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
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
regex count expression
asked Nov 13 '18 at 11:26
JamesJames
132
132
Just count the colons. Using python as an examplelen("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
add a comment |
Just count the colons. Using python as an examplelen("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
add a comment |
3 Answers
3
active
oldest
votes
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
1
That is great, just what I was after, thank you very much for your help.
– James
Nov 13 '18 at 16:16
add a comment |
A simple regex that matches strings containing 3 and only 3 colon:
^([^:]*:){3}[^:]*$
DEMO
add a comment |
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
>
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
1
That is great, just what I was after, thank you very much for your help.
– James
Nov 13 '18 at 16:16
add a comment |
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
1
That is great, just what I was after, thank you very much for your help.
– James
Nov 13 '18 at 16:16
add a comment |
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
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
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
add a comment |
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
add a comment |
A simple regex that matches strings containing 3 and only 3 colon:
^([^:]*:){3}[^:]*$
DEMO
add a comment |
A simple regex that matches strings containing 3 and only 3 colon:
^([^:]*:){3}[^:]*$
DEMO
add a comment |
A simple regex that matches strings containing 3 and only 3 colon:
^([^:]*:){3}[^:]*$
DEMO
A simple regex that matches strings containing 3 and only 3 colon:
^([^:]*:){3}[^:]*$
DEMO
answered Nov 13 '18 at 12:39
TotoToto
65.1k175698
65.1k175698
add a comment |
add a comment |
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
>
add a comment |
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
>
add a comment |
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
>
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
>
answered Nov 13 '18 at 11:33
stack0114106stack0114106
2,4161417
2,4161417
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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