0

I have the following regex in Java:

private static final String ALPHA_REGEX = "[^A-Za-z]+$";

If I input say "a334234234" the validation will fail. However if I input "a3423423fsfsdf" the validation succeeds...

Same goes for the following regex which checks whether the input is numeric only:

private final static    Pattern NUMBER_ONLY_PATTERN = Pattern.compile("[^0-9 ]+$", Pattern.CASE_INSENSITIVE);

If I input 4saasd the validation fails, but if I input 3dfsdf22 the validation is successful. Can you help me with this?

1 Answer 1

2

^ in [] mean not, but without [] it means beginning of string, this is your issue

^[a-zA-Z]+$ - only letter

^\d+$ - only digit

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

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.