[Java] Regular Expressions in Java ~Knowing about it~

less than 1 minute read

In java, sometimes an array or character is decomposed and stored in an array.

At that time, split is performed using the split method, but it is processed like this. (Variable name is appropriate)

private static final String SEPARATOR = "(\\s+?|\\.|,|;)";

String[] words = line.split(SEPARATOR);


What is “(\s+?|\.|,|;)”;?

These are regular expressions.

Specifically, it means “one or more spaces, periods, commas, or semicolons.”

| Means one of the delimiters, and \. is just a period (two backslashes are necessary for convenience of character string notation).

Depending on the OS, \ becomes \, or \ is typed twice but only one is displayed on this screen.

It was a learning (= ゜ω゜) no