Regex - Match repeated character not in sequence
Asked Answered
R

5

5

Is there a way to match repeated characters that are not in a sequence?

Let's say I'm looking for at least two 6s in the string.

var string = "61236";

I can only find quantifiers that match either zero or one or characters in a sequence.

River answered 16/1, 2014 at 16:17 Comment(2)
Can they be in sequence as well?Languishment
Yes. But does not have to.River
K
7

Here is a regexp that matches a character that is followed somehwere in the string by the same digit:

/(.)(?=.*?\1)/

Usage:

var test = '0612364';
test.match(/(.)(?=.*?\1)/)[0] // 6

DEMO: https://regex101.com/r/xU1rG4/4

Here is one that matches it repeated at least 3 times (total of 4+ occurances)

/(.)(?=(.*?\1){3,})/

DEMO: https://regex101.com/r/xU1rG4/3

Kitkitchen answered 16/1, 2014 at 16:21 Comment(8)
Maybe add a lazy qualifier so that the .* doesn't match all the way to the right before looking for a backreference match: /(\d)(?=.*?\1)/Croce
@MikeSamuel Added, thanks. I can't seem to add a maximum amount of occurances to the regex. Does anyone have any ideas? I tried [^\1] instead of ., but that is the character 1, tried {3,5}, but that just matches the \d with ..Kitkitchen
to match a maximum, you need to ensure that the .*? does not match \1: (?:(?!=\1).)*?Croce
Awesome! It works. Thank you! Damn you guys are fast.River
@MikeSamuel I tried that in the meantime, but it doesn't seem to work: regex101.com/r/iL2dQ1 Perhaps i made a stupid mistake?Kitkitchen
You should update your regex101 link. It does not show exactly the queries you are trying here now. I tried to fix it until I realized the earlier version was also incorrect.Mediative
@Mediative The original link was for the generic regex101 sample, so when you edited it and created a new version, the original link pointed towards your version. I fixed it and added another version for the last example.Kitkitchen
I didnt change the link at all or save a new version. The first DEMO still does not match the code you have show here (which I was trying to fix but became hesitant). It does match an earlier version of this answer though.Mediative
M
3

Have a try with this regex:

/(.).*?\1/
Mandatory answered 16/1, 2014 at 16:20 Comment(0)
A
0

Match 6 and anything up until another 6

!!"61236".match("6.*6")
// returns true

Match 6 and anything up until another the same as the first group (which is a 6)

!!"61236".match(/(6).*\1/)
// returns true
Annabelleannabergite answered 16/1, 2014 at 16:22 Comment(2)
What are those !! doing in the beginning? Is that js?River
@KarlPokus it converts the value returned by match to a boolean. If a match is found, the resulting array gets converted to true, otherwise null gets converted to false.Kitkitchen
M
0

Characters that are not in sequence and appear more than once:

/(.)(.+\1)+/
Mosasaur answered 16/1, 2014 at 16:34 Comment(0)
U
0

You can remove the ? after .* since the .* already matches zero or more characters.

(.)(?=.*\1)

var test = "0612364";
console.log(
  test.match(/(.)(?=.*\1)/)[0]
);
Underfoot answered 16/10 at 12:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.