6

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

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

I have consulted a couple of sources- like 1 and 2-to decipher what this code mean but I can't figure it out. Can anybody explain what the argument in the split() method means?

Edit: To anyone who has the same question as I had, here's another helpful link

Community
  • 1
  • 1
Haque1
  • 575
  • 1
  • 11
  • 21

2 Answers2

4

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

ABC

then the matches would occur at |, between the characters.

A|B|C|
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I know that "." matches any character except for line breaks, but what does "<=" mean in this context? – Haque1 May 20 '13 at 02:25
  • 1
    @Haque1 That's a sequence of metacharacters that tells the expression that the "." should not *capture* anything. The engine needs to see that a character is there, but it should not remove that character from the stream. – Sergey Kalinichenko May 20 '13 at 02:36
  • 1
    @Haque1 This is an unusual use of lookbehind. A more common way is like this: `(?<=tag:)"[^"]*"`. This expression matches a quoted string only when it is preceded by a `tag:` string. – Sergey Kalinichenko May 20 '13 at 02:37
1

.split("") (on an empty string/pattern) will match the empty string at the start of the regex. This is an additional empty string character that is undesirable. (?<=.) is a zero-width assertion (does not consume any characters) that matches the zero-width space followed by any character (followed by because it is a lookbehind). This splits on the empty string between each character, but not the empty space between the first character and the start of the string.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405