How do i find the sequence of digit in a long string which is numbers only p5 js

164 views Asked by At

I have this problem. How can i write code that would search for a sequence of digits in a string that is numbers only. For instance for this string 123124567 i want to search for n-digit sequence and if it repeats one or more time show it.

I'll show it on example of what i want to achive For 123124567 Show all 2-digit sequences (that are in a row) so output should be 12 23 31 12 24 45 56 67 And from it show those that repeat one or more time in this case 12

Im r begginger but i would like to teach myself to to this as i am working on bigger project where i need to show every number sequences in very long string. I tried to use the Regular Expressions for it but then i got the results like: 12 31 24 56 and it wasnt checking every possible two digits sequence like 12 23 31 12 24 45 56 67

var textfield;
var output;
var submit;

function setup() {
  noCanvas();
  textfield = select("#input");
  output = select('#output');
  submit = select("#submit");
  submit.mousePressed(newText);
}

function newText() {
  var s = textfield.value();

  var r = /\d\d/g; //here i would need to adjust it so to given digit length
  var matches = s.match(r);

  for (var i = 0; i < matches.length; i++) {
    createP(matches[i]);
   //at this point the results are wrong not showing all possible sequences
  }

  }
1

There are 1 answers

19
Kevin Workman On BEST ANSWER

If you really want to use a regular expression, then I'd recommend googling "regular expression find repeated substrings" for a ton of results.

Or you could simplify your approach and do the matching yourself. Break the problem down into smaller sub-steps: first write code that splits your String up. Then add each two-digit number into a data structure and check for repeats as you add them.