37

I want such a validation that My String must be contains at least one alphabet.

I am using the following:

String s = "111a11";
boolean flag = s.matches("%[a-zA-Z]%");

flag gives me false even though a is in my string s

Srinivas
  • 1,770
  • 1
  • 14
  • 27
Jignesh Ansodariya
  • 12,103
  • 22
  • 76
  • 108
  • 7
    Don't use %. That is for SQL LIKE, not regexp. Use `.*` instead So just `s.matches(".*[a-zA-Z].*");` – ppeterka Jan 11 '13 at 12:27

2 Answers2

98

You can use .*[a-zA-Z]+.* with String.matches() method.

boolean atleastOneAlpha = s.matches(".*[a-zA-Z]+.*");
Bhesh Gurung
  • 49,592
  • 20
  • 90
  • 140
24

The regular expression you want is [a-zA-Z], but you need to use the find() method.

This page will let you test regular expressions against input.

Regular Expression Test Page

and here you have a Java Regular Expressions tutorial.

Java Regular Expressions tutorial

Kevin Panko
  • 8,069
  • 19
  • 50
  • 60
Luciano
  • 598
  • 9
  • 21