Jeff Archer Jeff Archer -4 years ago 77
Java Question

Regex to validate words do not contain numbers or special characters

I am developing a java app, running on android. I am trying to pick all words which do not contain any embedded digits or symbols.

The best I have come up with is:

\b[a-zA-Z]+[a-zA-Z]*+\b


Test Data:

this is a test , an0ther gr8 WW##ee one, w1n 1test test1 end


This results in picking the following:
this, is, a, test, WW##ee, one, end


I need to eliminate the
WW##ee
from the results.

Answer Source

You shouldn't use a word boundary meta-character \b since it matches the position right after WW which sees a hash # character. This position is a word boundary itself. So you should pick up a different way:

(?<![\S&&[^,]])[a-zA-Z]+(?![\S&&[^,]])

Using character class intersection feature of Java's regex you are able to define punctuation characters that are allowed to follow or precede a word character. Here it is a comma ,.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download