0

I want to validate a string which donot have numeric characters.

If my string is "javaABC" then it must be validated If my string is "java1" then it must not be validated

I want to restrict all the integers.

1
  • This kind of question can easily be answered by creating a small test program. Commented Sep 23, 2009 at 10:58

5 Answers 5

6

Try this:

String  Text        = ...;
boolean HasNoNumber = Text.matches("^[^0-9]*$");

'^[^0-9]*$' = From Start(^) to end ($), there are ([...]) only non(^) number(0-9). You can use '\D' as other suggest too ... but this is easy to understand.

See more info here.

Sign up to request clarification or add additional context in comments.

3 Comments

Perhaps it's easier for a regexp newbie to understand hasDigits = Text.matches("[0-9]") ?
"^\\D*$" is a shorter form ;)
@Brian: Text.matches("[0-9]") means "Text" consists of exactly one digit; you would need to say Text.matches("(?s).*[0-9].*") -- Java's funny that way. OTOH, @NawaMan's regex doesn't really need the anchors: Text.matches("[^0-9]*") or Text.matches("\\D*") work just fine.
2

You can use this:

\D

"\D" matches non-digit characters.

Comments

2

More detail is here: http://java.sun.com/javase/6/docs/api/java/util/regex/Pattern.html

Comments

2

Here is one way that you can search for a digit in a String:

public boolean isValid(String stringToValidate) {
   if(Pattern.compile("[0-9]").matcher(stringToValidate).find()) {
       // The string is not valid.
       return false;
   }

   // The string is valid.
   return true;
}

Comments

1

The easiest to understand is probably matching for a single digit and if found fail, instead of creating a regexp that makes sure that all characters in the string are non-digits.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.