Reputation: 279
I'd like to combine the \w
and \s
classes. So the input should only contain letters, numbers and whitespaces.
I've already tried "[\\w\\s]
" and "\\w\\s
" and others. But they don't work.
Thanks in advance!
Upvotes: 0
Views: 1571
Reputation: 80384
You need (?U)[\p{alnum}\s]
, under Java 7. Otherwise it gets both those sets wrong, because it only works on ASCII otherwise.
Upvotes: 2
Reputation: 42020
Try this:
^[\w\s&&[^_]]+$
\w
other than letters and numbers, also inclues underscore. You need to do a subtraction if don't want it.
abc abc 123
: YESabc_abc 123
: NOUpvotes: 0
Reputation: 124225
Try maybe (\\w|\\s)
to combine it. It means \\w
OR
\\s
, but for me
System.out.println("abc def ghi".replaceAll("[\\w\\s]", "X"));
//out -> XXXXXXXXXXX
works fine
Upvotes: 0