Java Regex: Check for non word characters
I wanted to test a given string to see if it contains any non word characters in Java. Initially I came up with a lengthy version ;) Then after a bit of search I could simply shorten it.Version1: regular expression
"\\p{Alnum}*[~!@#$%^&*()\\+=\\-:;<>\\s?\\[\\]{},/\\\\\"]+\\p{Alnum}*"
Version2: regular expression
"\\p{Alnum}*\\W+\\p{Alnum}*"
Here's a nice guide (http://java.sun.com/docs/books/tutorial/essential/regex/index.html) to start with Java regular expressions
Subscribe to:
Post Comments
(
Atom
)
Does not work for string with more than one non-word character distributed through the string e.g. "word&&word&&word" will fail.
ReplyDeleteThe Pattern ".*\\W+.*" covers this case
Thanks Patrick. In fact I used this to validate key strokes. That's why I didn't catch the issue you have pointed out.
ReplyDelete