Is it possible to write a regex to test this:

Is it possible to write a regular expression to check if all numbers of a certain significant 10number were up to three times? for example, the return value for Regex.IsMatch("xxxx", "4433425425")is false. and for Regex.IsMatch("xxxx", "4463322545")true. What is xxxx? in the first I had 4 occurrencenumbers 4, and in the second - numbers no more than 3once.

+3
source share
3 answers

Will match any digit with four or more copies

 string found =  Regex.Match(s,@"(\d).*\1.*\1.*\1").Groups[1].Value;

Just an example of how to use it.

static void Main( string[] args )
{
     string fail = "1234567890";
     string s = "1231231222";
     string mTxt = @"(\d).*\1.*\1.*\1";
     Console.WriteLine( Regex.Match(s,mTxt).Success);
     Console.WriteLine(Regex.Match(fail, mTxt).Success);
}

Taken on @Brads Comments below use

([0-9]).*\1.*\1.*\1
+7
source

Find a number three times in a row:

(?=(0{3}|1{3}|2{3}|3{3}|4{3}|5{3}|6{3}|7{3}|8{3}|9{3}).{3}

, :

(.?0.?){3}|(.?1.?){3}|(.?2.?){3}|(.?3.?){3}|(.?4.?){3}|(.?5.?){3}|(.?6.?){3}|(.?7.?){3}|(.?8.?){3}|(.?9.?){3}

(C/O @rerun):

([0-9]).*\1.*\1.*

. . 10 . , .

+3

downvotes , , , .

, , "" or, , , , , . Pseudo-code - :

def isValid (str):
    foreach ch in '0'..'9':
        count[ch] = 0
    foreach ch in str:
        if ch not in '0'..'9':
            return false
        count[ch] = count[ch] + 1
    foreach ch in '0'..'9':
        if count[ch] > 3:
            return false
    return true

What is my advice, take it or leave it, I will not be offended :-)

+1
source

Source: https://habr.com/ru/post/1770935/


All Articles