aspose file tools*
The moose likes Java in General and the fly likes Check if a String has alphabets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Check if a String has alphabets" Watch "Check if a String has alphabets" New topic
Author

Check if a String has alphabets

Nikhil Das Nomula
Greenhorn

Joined: Jun 10, 2011
Posts: 23
I have a string which contains numbers and characters or just numbers alone or characters alone and I want to check if it contains alphabets. I think we have to use regular expressions but I am not sure how do I go about with it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

What have you tried?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Eoin Brosnan
Ranch Hand

Joined: Dec 12, 2009
Posts: 37
Nikhil Das Nomula wrote:I have a string which contains numbers and characters or just numbers alone or characters alone and I want to check if it contains alphabets. I think we have to use regular expressions but I am not sure how do I go about with it.




I am not clear on what pattern you want to match but the above is a starting point. [a-zA-Z] will match any letter, upper or lower.
Nikhil Das Nomula
Greenhorn

Joined: Jun 10, 2011
Posts: 23
@Fred rosenberger : I have tried iterating through each character but I wanted to use regular expressions.

@Eoin Brosnan: The characters can be at any place, they can be at the starting point, at the ending point or occur in the middle. Something like 989BR09 or 3988BR or SA3432DAS
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

And did you try that code with your input examples?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Nikhil Das Nomula
Greenhorn

Joined: Jun 10, 2011
Posts: 23
I tried that code but if we pass a string which has only number then it fails.

Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher("3453443534534");

if(m.find())
System.out.println("contains letters");

Output:

contains letters
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

I am not a regex expert, but you may need a '+' on your pattern to say "one or more"

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Nikhil Das Nomula wrote:Output:

contains letters

Odd. I just copy-pasted that code and get no output at all, as expected.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

fred rosenberger wrote:I am not a regex expert, but you may need a '+' on your pattern to say "one or more"


Not for find(). You would for matches(), if you need to test that the whole String consists of characters [a-zA-Z]. To ascertain the presence of at least one alpha character using matches(), this will suffice.


luck, db
There are no new questions, but there may be new answers.
Nikhil Das Nomula
Greenhorn

Joined: Jun 10, 2011
Posts: 23
@Rob Spoor: Here is the code that I tested with


public class RegularExpressionTest {
public static void main(String args[]){

Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher("3453443VD534534");

if(m.find())
System.out.println("contains letters");
else{
System.out.println("no letters");
}
}
}

Nikhil Das Nomula
Greenhorn

Joined: Jun 10, 2011
Posts: 23
@Darryl Burke I tried doing that and it works !! Thanks a lot !
Nikhil Das Nomula
Greenhorn

Joined: Jun 10, 2011
Posts: 23
Just FYI here is the code that actually worked. Thanks to Darryl Burke

Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

The .* thing is not strictly needed when using find() (as opposed to match()).

This code works just fine:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check if a String has alphabets
 
Similar Threads
regex problem
Special characters in Java
Problem using Pattern n Matcher (REGEX)
string validation
Problem using Pattern and Matcher (REGEX)