Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a string on whitespaces

Tags:

java

string

regex

I'm currently trying to splice a string into a multi-line string. The regex should select white-spaces which has 13 characters before.

The problem is that the 13 character count does not reset after the previous selected white-space. So, after the first 13 characters, the regex selects every white-space.

I'm using the following regex with a positive look-behind of 13 characters:

(?<=.{13}) 

(there is a whitespace at the end)

You can test the regex here and the following code:

import java.util.ArrayList;
public class HelloWorld{

     public static void main(String []args){
        String str = "This is a test. The app should break this string in substring on whitespaces after 13 characters";

        for (String string : str.split("(?<=.{13}) ")) {
            System.out.println(string);
        }
     }
}

The output of this code is as follows:

This is a test.
The
app
should
break
this
string
in
substring
on
whitespaces
after
13
characters

But it should be:

This is a test.
The app should
break this string
in substring on
whitespaces after
13 characters

like image 229
Seblor Avatar asked May 04 '16 20:05

Seblor


People also ask

How do you split a string on the basis of whitespace?

You can split a String by whitespaces or tabs in Java by using the split() method of java. lang. String class. This method accepts a regular expression and you can pass a regex matching with whitespace to split the String where words are separated by spaces.

How do you split a string in whitespace in Python?

The Pythonic way of splitting on a string in Python uses the str. split(sep) function. It splits the string based on the specified delimiter sep . When the delimiter is not provided, the consecutive whitespace is treated as a separator.

How do I split a string without a separator?

Q #4) How to split a string in Java without delimiter or How to split each character in Java? Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.

How split a string by space in C#?

String. Split can use multiple separator characters. The following example uses spaces, commas, periods, colons, and tabs as separating characters, which are passed to Split in an array . The loop at the bottom of the code displays each of the words in the returned array.


1 Answers

You may actually use a lazy limiting quantifier to match the lines and then replace with $0\n:

.{13,}?[ ]

See the regex demo

IDEONE demo:

String str = "This is a test. The app should break this string in substring on whitespaces after 13 characters"; 
System.out.println(str.replaceAll(".{13,}?[ ]", "$0\n"));

Note that the pattern matches:

  • .{13,}? - any character that is not a newline (if you need to match any character, use DOTALL modifier, though I doubt you need it in the current scenario), 13 times at least, and it can match more characters but up to the first space encountered
  • [ ] - a literal space (a character class is redundant, but it helps visualize the pattern).

The replacement pattern - "$0\n" - is re-inserting the whole matched value (it is stored in Group 0) and appends a newline after it.

like image 158
Wiktor Stribiżew Avatar answered Nov 09 '22 06:11

Wiktor Stribiżew