How to set java delimiters with ", /*, \*

Viewed 89

I am writing a code that is going to read string and has to know if it is made up of letters or is a punctuation or not. for eq: (string refers to ( and string as 2 different entities.

I have already set a delimiter with

String DELIM = "[\\s\\-\\.\\'\\,\\?\\,\\_\\@\\#\\(\\)\\;]+";

but code still treats , *, / as string made of letters rather than punctuation.

How could I edit my Delim to be able to change that?

1 Answers

Inside square brackets block [here] you don't need to escape too much.
Regex [\\s-.'?,_@#();*\\/]+ perfectly catches any delimiter in string
aa bb-cc.dd'ee?ff,gg_hh@ii#jj(kk)ll;mm*nn/oo.

Here is a Java snippet:

String regex = "[\\s-.'?,_@#();*\\/]+";
String text = "aa bb-cc.dd'ee?ff,gg_hh@ii#jj(kk)ll;mm*nn/oo";
String[] split = text.split(regex);
for (String s : split) {
    System.out.println(s);
}

Output:
aa
bb
cc
...

You could also go the other way and catch anything that is NOT a latter or a digit with regexp:
[^a-zA-Z0-9]+.

Related