Reputation: 21
I'm currently new to Java and would like some help on understanding this line of code, what does this regular expression do/mean.
String[] s = l.split("[\\p{Punct}\\s]+");
Upvotes: 1
Views: 1772
Reputation: 1916
The split function will return an array of String, based on the separation characters you specify in the given pattern.
According to the Java Pattern documentation: Class Pattern, the relevant separation symbols you used are:
Thus, the array of string will be made of all Strings which are separated every time one of the above characters are found in you whole string.
Here is an example you can use to test it:
import java.util.*;
public class TestSplit {
public static void main(String args[]) {
String myStringTest = "test,of#the@split&separated by(space)and+punctuations";
List<String> list = Arrays.asList(myStringTest.split("[\\p{Punct}\\s]+"));
System.out.println(list);
}
}
And here is the output you get:
[test, of, the, split, separated, by, space, and, punctuations]
Upvotes: 2