tags:

views:

110

answers:

4

hi I want a regex pattern to check that the string doesn't contain any digits .

For Illustration : I'm coding a Hospital System and I want from user to enter the name of the patient , Of course the name shouldn't contain any digits , how can I do this by Regex ?

+2  A: 

Try: \D

See the JavaDoc for Pattern

Fried Hoeben
+6  A: 

\D is a non-digit, and so then \D* is any number of non-digits in a row. So your whole string should match \D*.

MatrixFrog
That regex is fine if you're using the `matches()` method. If you use `find()` you'll always get a match, digits or no. If for some reason you had to use `find()` you would need to anchor the regex: `^\D*$`
Alan Moore
+1  A: 

Well names usually alphabetical, so this will be good : [a-b,A-Z, ,-]+

EDIT : [a-zA-Z- ',]+

fastcodejava
what about names that include an apostrophe?
jball
Why do you have all those commas in there? All they do is match a literal comma in the target string. And what's with the range `a-b`? Isn't that supposed to be `a-z`?
Alan Moore
@Alan, Corrected typo.
fastcodejava
ASCII is not the only *charset*; what about names with 'ç', 'ã', 'ä', 'à', 'œ',...
Carlos Heuberger
+2  A: 

\A\D*\Z

a good regex cheatsheet here

Arthur Thomas