简体   繁体   中英

Java Regex Metacharacters

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

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|

.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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM