Java Regex Metacharacters

I found this thread, and one of the users posted the following line of code on it:

String[] digits2 = number.split("(?<=.)"); 

I consulted with several sources, such as 1 and 2 - to understand what this code means, but I cannot understand. Can someone explain what the argument in the split () method means?

Edit : for everyone who has the same question as mine, here is another useful link

+6
source share
2 answers

This is a positive lookbehind . The general expression means "after any character, but without capturing anything." Essentially, if the line looks like

 ABC 

then matches will occur in | between the characters.

 A|B|C| 
+4
source

.split("") (in an empty line / template) will match an empty line at the beginning of the regular expression. This is an optional empty string character that is not desired. (?<=.) is a zero-width statement (doesn't use any characters) that matches a zero-width space, followed by any character (which follows because it's lookbehind). This is split into an empty line between each character, but not the empty space between the first character and the start of the line.

+1
source

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


All Articles