Regex breaks into overlapping lines

I am exploring the possibilities of regular expressions, so I'm just wondering if something like this is possible:

public class StringSplit {
    public static void main(String args[]) {
        System.out.println(
            java.util.Arrays.deepToString(
                "12345".split(INSERT_REGEX_HERE)
            )
        ); // prints "[12, 23, 34, 45]"
    }
}

If possible, just include a regex (and a proactive explanation of how it works).

If this is only possible with some variations of regular expressions other than Java, then also feel free to provide them.

If this is not possible, please explain why.


BONUS QUESTION

The same question, but with a loop find()instead split:

    Matcher m = Pattern.compile(BONUS_REGEX).matcher("12345");
    while (m.find()) {
        System.out.println(m.group());
    } // prints "12", "23", "34", "45"

, , , . , , ; , , ( , " " ).

, .

+3
5

, split(), find() . lookahead :

Matcher m = Pattern.compile("(?=(\\d\\d)).").matcher("12345");
while (m.find())
{
  System.out.println(m.group(1));
}

, , lookahead lookbehind, , . , "" .

, , . ("(?=(\\d\\d))"), . , , , , , .

split() , , , Java. lookarounds , .

+5

Matcher.find split , , for , ( ):

import java.util.*;
import java.util.regex.*;

public class StringSplit { 
    public static void main(String args[]) { 
        ArrayList<String> result = new ArrayList<String>();
        for (Matcher m = Pattern.compile("..").matcher("12345"); m.find(result.isEmpty() ? 0 : m.start() + 1); result.add(m.group()));
        System.out.println( result.toString() ); // prints "[12, 23, 34, 45]" 
    } 
} 

EDIT1

match(): , , BONUS_REGEX, Matcher, , (.. ), , , .. (. ). BONUS_REGEX "(.\\G.|^..)", , , \G -anchor-in-the-middle Java Match ( Perl):

 perl -e 'while ("12345"=~/(^..|.\G.)/g) { print "$1\n" }'
 12
 23
 34
 45

split(): INSERT_REGEX_HERE (?<=..)(?=..) ( - , ), , split , [12, 3, 45] ( , .)

EDIT2

split(), , , ( ):

Pattern.compile("((?<=.).(?=.))").matcher("12345").replaceAll("$1#$1").split("#")

, , ( look-back) ; , ( ), , :

Pattern.compile("((?<=.).(?=.))").matcher("12345").replaceAll("$1$1").split("(?<=..)(?=(..)*$)")

match() ( ):

Matcher m = Pattern.compile("..").matcher(
  Pattern.compile("((?<=.).(?=.))").matcher("12345").replaceAll("$1$1")
);
while (m.find()) { 
    System.out.println(m.group()); 
} // prints "12", "23", "34", "45" 
+4

, . , .

+1

, split(), , .

In Perl, this works:

my $string = '12345';
my @array = ();
while ( $string =~ s/(\d(\d))/$2/ ) {
    push(@array, $1);
}
print join(" ", @array);
# prints: 12 23 34 45

The find-and-replace expression says: match the first two adjacent digits and replace them in the string with only the second of the two digits.

+1
source

Alternative using simple Perl mapping. Should work wherever they do. And there is no need for a loop.

 $_ = '12345';
 @list = /(?=(..))./g;
 print "@list";

 # Output:
 # 12 23 34 45

But this one, as previously published, is better if the \ G trick works:

 $_ = '12345';
 @list = /^..|.\G./g;
 print "@list";

 # Output:
 # 12 23 34 45

Edit : Sorry, did not notice that all this has already been published.

0
source

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


All Articles