2

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

Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
Srikar
  • 351
  • 5
  • 16

2 Answers2

4

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.

Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
3

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`
Mojtaba Yeganeh
  • 2,788
  • 1
  • 30
  • 49