1

I have a string formated like this:

String str = "AA.BBB..CC.DDDD...EE....F.G..H";

And I want to split this string by a dot with this as output:

AA
BBB
.CC
DDDD
.
EE
..
F
G
.H

str.split("\\.") of course did not work.

5
  • Why is there two dots in between E and F, in sequence? Shouldn't be those on two separate lines? Commented Nov 24, 2012 at 15:29
  • I think he wants every substring that starts and ends with a dot. Something like ".*." and, in case of multiple dots at the beginning of the substring, he takes the first one as separator and includes the others in the string. Commented Nov 24, 2012 at 15:37
  • @DanielaMogini.. In that case the output for DDDD...EE....F does not follow. Commented Nov 24, 2012 at 15:39
  • @Enroy.. Please specify on what basis you want the split. It is not clear from the output you posted. Commented Nov 24, 2012 at 15:40
  • One dot : handle it like split("\\.") Two dots : take the first as separator, include 2nd dot in string 3+ dots : take outer dots as separator, and take everything between those as new string. Commented Nov 25, 2012 at 20:08

1 Answer 1

3

This should work:

str.split("(?<!\\.)\\.|(?<=\\.\\.)\\.(?!\\.)")

The string should be split in these 2 cases:

  • The . is not preceded by another .
  • The . is not succeeded by ., and is preceded by 2 consecutive ..
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Was very near to it. But couldn't get the exact output. Was missing (?<=\\.\\.) in the 2nd part. :)

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.