Home » Tip Bank » Java
  • Comment on this Article
Language: Java
Expertise: Intermediate
Apr 23, 2019
  • facebook
  • twitter
  • in
  • Email

WEBINAR:

On-Demand

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


Watch

Regex Has a Presence in Java as Well

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

*/
M S Sridhar
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
codeschool.com
codeschool.com