tags:

views:

209

answers:

4

Examples:

  1. "1 name": Should say it has characters
  2. "10,000": OK
  3. "na123me": Should say it has characters
  4. "na 123, 000": Should say it has characters
A: 

With this line you can check if your string contains only of characters given by the regex (in this case a,b,c,...z and A,B,C,...Z):

boolean doesMatch = "your string".matches( "[a-zA-Z]*" );
tangens
that regex would match any string that has at least one alphabetic character
Kip
I thought it would match any string that had precisely one alphabetic character, not just at least one. Doesn't it need an asterisk?
Paul
Thanks, I added one.
tangens
The question is unclear enough that you're both right.
WhirlWind
This tests only if there is one character in a string
Vishal
Doesnt * match zero to many? It should be + instead.
Shervin
With the asterisk, it will also match an empty string.
mpez0
This doesn't work. Java regexes are by default bounded by `^` and `$`. Test with `1name` yourself.
BalusC
+2  A: 
 public static void main(String[] args)

 {
     Pattern p = Pattern.compile("^([^a-zA-Z]*([a-zA-Z]+)[^a-zA-Z]*)+$");
     Matcher m = p.matcher("1 name");
     Matcher m1 = p.matcher("10,000");
     Matcher m2 = p.matcher("na123me");
     Matcher m3 = p.matcher("na 123, 000");
     Matcher m4 = p.matcher("13bbbb13jdfgjd43534 fkgdfkgjk34 rktekjg i54 ");

     if (m.matches())
         System.out.println(m.group(1));

     if (m1.matches())
         System.out.println(m1.group(1));

     if(m2.matches())
         System.out.println(m2.group(1));

     if(m3.matches())
         System.out.println(m3.group(1));

     if (m4.matches())
         System.out.println(m4.group(1));
 }

The above should match any letter in both lower and upper case. If the regex returns a match, the string has a letter in it.

Result

1 name

me

na 123, 000

i54

Statements that contain no letters do not match the expression.

npinti
If it matches, the string has *only* letters (A-Z) in it.
Mark Byers
Should be fixed now. Thanks for letting me know.
npinti
Fails with input: "11"
OscarRyz
Regular expressions that begin with '^.*' or end with '.*$' are poorly written. Why not just use the regular expression '[a-zA-Z]'?
David M
@David: unlike in PHP, Perl and another languages, Java regexes are **implicitly** bounded by `^` and `$`.
BalusC
@BalusC, incorrect! The `matches()` method uses the whole string, but the `find()` method does not.
David M
@David: I am not that experienced, how ever just because with Java the ^ and $ are implicitly added, I do not think that they should be left out. Putting in an extra character or two won't harm, but I think it makes the code more understandable.
npinti
@David: yes, in `matches()`, that's also what I mean.
BalusC
+3  A: 
public class HasCharacters  {
    public static void main( String [] args ){
        if( args[0].matches(".*[a-zA-Z]+.*")){
            System.out.println( "Has characters ");
        } else {
            System.out.println("Ok");   
        }
    }
}

Test

$java HasCharacters "1 name" 
Has characters 
$java HasCharacters "10,000"
Ok
$java HasCharacters "na123me"
Has characters 
$java HasCharacters "na 123, 000" 
Has characters 
OscarRyz
This doesn't work. Java regexes are by default bounded by `^` and `$`. Test with `1name` yourself.
BalusC
It fails for the first example
Vishal
Fixed and tested B-)
OscarRyz
+2  A: 

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.

http://www.fileformat.info/tool/regex.htm

David M