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

2 Answers 2

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|
Sign up to request clarification or add additional context in comments.

3 Comments

I know that "." matches any character except for line breaks, but what does "<=" mean in this context?
@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.
@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.
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.

3 Comments

Zero-width space is the wrong term to use here; that's a different thing.
@Cairnarvon thanks for pointing this out ... which term would you prefer?
I want to say "character boundary", but that's not strictly right either. I'm not sure, to be honest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.