-1

I need to get a text splitted with regex in Java (each substring will be less than or close to 10 characters (including space and special) and no word would be splitted). For example, "James has gone out for a meal." would be "James has", "gone out", "for a meal", ".". Thanks in advance.

Manoj Vadehra
  • 836
  • 4
  • 17

2 Answers2

1

This expression might be a little complicated, maybe we could start with:

.{1,10}[^\s](?=\s|$)

DEMO

import java.util.regex.Matcher;
import java.util.regex.Pattern;

final String regex = ".{1,10}[^\\s](?=\\s|$)";
final String string = "James has gone out for a meal.";

final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
    System.out.println("Full match: " + matcher.group(0));
    for (int i = 1; i <= matcher.groupCount(); i++) {
        System.out.println("Group " + i + ": " + matcher.group(i));
    }
}

RegEx Circuit

jex.im visualizes regular expressions:

enter image description here

Emma
  • 27,428
  • 11
  • 44
  • 69
1

First, remove all double spaces if exists and apply this regex.

.{1,11}(?:\s|$)|.{1,11}(?:[^\s]|$)

But I would use the split function and then 'for clause' calculating lengths.

Kang Andrew
  • 330
  • 2
  • 14