Matching incremental digits to digits

After much discussion about the problem, finally, I post this question here and hope that it will be solved by experts here; I am looking for a regex pattern that can match incremental backlinks. Let me explain:

For the number, the 9422512322pattern (\d)\1will match 22twice, and I want a pattern (something like (\d)\1+1) that matches 12( second digitequals first digit + 1),

In short, the pattern must match all the events, such as 12, 23, 34, 45, 56and etc .... There is no substitute, simply required compliance.

+4
source share
4 answers

How about this?

/01|12|23|34|45|56|67|78|89/

It is not sexy, but he is doing his job.

+6
source

You can use this regex:

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

This will match:

  • Any 0followed by 1s, or
  • Any 1followed by 2s, or
  • Any 2followed by 3s, ...

A few times +, then map the corresponding symbol ..

Here is the regex demo , but a coincidence:

12345 555 5678 77 78 5
+1
source

Perl, . . .

PCRE , Perl.
(. , ( \d ) (?=( \d ))
  print "Overlap Found $1$3\n";

Perl, , .

- !

Perl:

use strict;
use warnings;

my $dig2;
while ( "9342251232288 6709090156" =~
          /
               (
                    ( \d )
                    (?{ $dig2 = $^N + 1 })
                    ( \d )
                    (?(?{
                         $dig2 != $^N
                      })
                         (?!)
                    )
               )
          /xg )
{
    print "Found  $1\n";
}

:

Found  34
Found  12
Found  67
Found  01
Found  56
0

Perl, :

#!/usr/bin/env perl

use strict;
use warnings;

my $number = "9422512322";

my @matches = $number =~ /(0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9))/g;

# We have only matched the first digit of each pair of digits.
# Add "1" to the first digit to generate the complete pair.
foreach my $first (@matches) {
  my $pair = $first . ($first + 1);
  print "Found: $pair\n";
}

:

Found: 12
Found: 23
0

All Articles