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

8
  • 1
    I mean you want to split on length 2, 4 and 7 ?? Commented Nov 29, 2017 at 5:18
  • i dont know about regex for different length but you can do it using substring inside any loop. Commented Nov 29, 2017 at 5:19
  • 1
    is there a pattern of length like 2,2,3,2,2,3.... ?? Commented Nov 29, 2017 at 5:29
  • 1
    You need to say what is so regular about pattern you want to find, or delimiter you want to match. Without it we can't really help you use regex for your problem. Commented Nov 29, 2017 at 5:41
  • @Pshemo It's like there are a lot of strings like the example above and I want to split them into 2,2,3,2 length and every string has exactly 9 numbers that's the pattern. Commented Nov 29, 2017 at 6:01

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You are a wizard @YCF_L :D hats off to you !
yes. though I assumed that. But its better to mention. thanks.
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`

Comments

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.