Regex for finding duplicate patterns in a phone number? Or maybe not?

I want to find numbers with a pattern from a list of several thousand, and I would like to match things like:

  • Sequence 3 or more, any direction (345.2345.876)
  • The repeating sequence (2233.3355.77777) 147.258.369 or the reverse thereof.
  • Repeating pattern (373737,33773377)

In a JSON object, for example:

["ok", {"series/020" : ["02034353637", "02034445673", "02034147369", "02034653185"]}]

Thus, they will all be consistent; for example, the former has 34 35 36, the latter has both 444 and 567, the latter has both 147 and 369, etc.

What have i tried? Well, most of the sites in this small box dived to my right! Search on the stack. Some online books ... but I was starting to wonder if there is such a thing as a “regular expression of dyslexia” (regexia?) Or maybe just thickness when I found a 142 page book . I know when they beat me.

Looking back, it might be a lot faster and easier to just do an "array of arrays" manually, creating templates using Google Sheets autocomplete, but is that possible?

Finally, is there "pay me $ 10, and I will do your regular expression for you"? If not, they should be! They will make a mint! Thanks.

+4
source share
2

RegEx, :

var data = ["ok", {
  "series/020": [
    "02034353637", "02034445673", "02034147369", "02034653185",
    "345", "2345", "876",
    "2233", "3355", "77777",
    "147", "258", "369",
    "373737", "33773377",
  ]
}]

var numbers = data[1]["series/020"]

var patternNumbers = numbers.filter(isPatternNumber)

// Demo output
document.write("<pre>" + JSON.stringify(patternNumbers, null, "\t") + "</pre>")
document.write("Matched " + patternNumbers.length + " out of " + numbers.length)

function isPatternNumber(n) {
  // constant pattern
  if (/147|258|369/.test(n)) return true

  // repeating pattern 3+
  if (/(?=(\d))\1{3,}/.test(n)) return true

  // sequence asc 3+
  if (/012|123|234|345|456|567|678|789/.test(n)) return true
    // sequence desc 3+
  if (/987|876|765|654|543|432|321/.test(n)) return true

  // repeating double sequence xxyy (note that x=y is possible, same as 4 in a row)
  if (/(?=(\d))\1{2}(?=(\d))\2{2}/.test(n)) return true

  // alternerting pattern xyxy (note that x=y is possible, => same as 4 in a row)
  if (/(?=(\d))\1{1}(?=(\d))\2{1}(?=(\d))\1{1}(?=(\d))\2{1}/.test(n)) return true

  return false
}

// Demo Input
var input = document.getElementsByTagName("input")[0]
var output = document.getElementsByTagName("span")[0]
input.oninput = function() {
  output.textContent = isPatternNumber(input.value)
}
Test a number:
<input type="text" /> <span></span>
Hide result
+3

(\d+?)\1+

, /

, . JS- litte

function sequencedDigitsRegex(minLength, maxLength){
    //the last digit
    var t = "'+a[0]+'?";
    //the optional Part
    for(var i=1; i<maxLength - minLength; ++i)
        t = "(?:'+a["+(i%10)+"]+'" + t + ")?";

    //the required part
    for(t = "'" + t + "'"; minLength>0; --minLength)
        t = "a["+(i++%10)+"] + " + t;

    //a template-method
    var template = new Function("a", "return " + t);
    var digits = "98765432109876543210".split("");

    //the regex-parts
    var out = new Array(20);
    for(var i=0; i<10; ++i){
        var a = digits.slice(i, i+10);
        out[i] = template(a);
        //also include the reversed variant
        out[i+10] = template(a.reverse());
    }
    //join the parts into one, long regex
    return new RegExp("("+out.join("|")+")", "g")
}   

, sth. :

var repeatingPattern = /(\d+?)\1+/g;   
var sequencePattern = sequencedDigitsRegex(2,8);
var patterns = new RegExp(repeatingPattern.source + "|" + sequencePattern.source, "g");
+1

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


All Articles