Jack Shore
Jack Shore

Reputation: 21

What does the regular expression "[\\p{Punct}\\s]+" do in Java?

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

Answers (1)

Marco Tizzano
Marco Tizzano

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:

  • \p{Punct} Punctuation: One of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~
  • \s A whitespace character: [ \t\n\x0B\f\r]

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

Related Questions