dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Regex Has a Presence in Java as Well

See how to use this form of Regex to identify whether or not a string has only alphabets present.


advertisement

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


See how to use this form of Regex below to identify whether or not a string has only alphabets present.

public class RegexHasOnlyAlphabets
{
   
   public static void main(String args[]) {
      RegexHasOnlyAlphabets regexHasOnlyAlphabets = new RegexHasOnlyAlphabets();
      regexHasOnlyAlphabets.proceed();
   }

   public void proceed() 
   {
      String inputStr = "HasOnlyAplhabets";
      System.out.println("Has only alphabets: " + inputStr + ": " + checkIfInputHasOnlyAlphabets(inputStr));

      inputStr = "HasOnly3Aplhabets";
      System.out.println("Has only alphabets: " + inputStr + ": " + checkIfInputHasOnlyAlphabets(inputStr));

   }      
   
   public boolean checkIfInputHasOnlyAlphabets(String strArg) 
   {
      return 
         (
            (null != strArg) && 
         (! strArg.equals("")) &&
         (strArg.matches("^[a-zA-Z]*$"))
         ); 
   }    
   
}

/*

Expected output:

[root@mypc]# java RegexHasOnlyAlphabets
Has only alphabets: HasOnlyAplhabets: true
Has only alphabets: HasOnly3Aplhabets: false

*/

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Thanks for your registration, follow us on our social networks to keep up-to-date