• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Check if a String has alphabets

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And did you try that code with your input examples?
 
Nikhil Das Nomula
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Darryl Burke I tried doing that and it works !! Thanks a lot !
 
Nikhil Das Nomula
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just FYI here is the code that actually worked. Thanks to Darryl Burke

 
Ranch Hand
Posts: 227
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The .* thing is not strictly needed when using find() (as opposed to match()).

This code works just fine:

 
Create symphonies in seed and soil. For this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic