This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check if a String has alphabets

 
Nikhil Das Nomula
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried?
 
Eoin Brosnan
Ranch Hand
Posts: 42
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And did you try that code with your input examples?
 
Nikhil Das Nomula
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12021
25
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not a regex expert, but you may need a '+' on your pattern to say "one or more"

 
Rob Spoor
Sheriff
Pie
Posts: 20381
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nikhil Das Nomula
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Darryl Burke I tried doing that and it works !! Thanks a lot !
 
Nikhil Das Nomula
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just FYI here is the code that actually worked. Thanks to Darryl Burke

 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic