Srikar
Srikar

Reputation: 371

Split String at different lengths in Java

I want to split a string after a certain length.

Let's say we have a string of "message"

123456789

Split like this :

"12" "34" "567" "89"

I thought of splitting them into 2 first using

"(?<=\\G.{2})" 

Regexp and then join the last two and again split into 3 but is there any way to do it on a single go using RegExp. Please help me out

Upvotes: 2

Views: 3349

Answers (2)

Shafin Mahmud
Shafin Mahmud

Reputation: 4071

Use ^(.{2})(.{2})(.{3})(.{2}).* (See it in action in regex101) to group the String to the specified length and grab the groups as separate Strings

  String input = "123456789";
  List<String> output = new ArrayList<>();

  Pattern pattern = Pattern.compile("^(.{2})(.{2})(.{3})(.{2}).*");
  Matcher matcher = pattern.matcher(input);

  if (matcher.matches()) {
     for (int i = 1; i <= matcher.groupCount(); i++) {
         output.add(matcher.group(i));
     }
  }

  System.out.println(output);

NOTE: Group capturing starts from 1 as the group 0 matches the whole String

And a Magnificent Sorcery from @YCF_L from comment

  String pattern = "^(.{2})(.{2})(.{3})(.{2}).*";
  String[] vals = "123456789".replaceAll(pattern, "$1-$2-$3-$4").split("-");

Whats the magic here is you can replace the captured group by replaceAll() method. Use $n (where n is a digit) to refer to captured subsequences. See this stackoverflow question for better explanation.

NOTE: here its assumed that no input string contains - in it. if so, then find any other character that will not be in any of your input strings so that it can be used as a delimiter.

Upvotes: 4

Mojtaba Yeganeh
Mojtaba Yeganeh

Reputation: 2922

test this regex in regex101 with 123456789 test string.

^(\d{2})(\d{2})(\d{3})(\d{2})$

output :

Match 1
Full match  0-9 `123456789`
Group 1.    0-2 `12`
Group 2.    2-4 `34`
Group 3.    4-7 `567`
Group 4.    7-9 `89`

Upvotes: 3

Related Questions