
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Regular expression to check if a string contains alphabet
Following is the regular expression to match alphabet in the given input −
"^[a-zA-Z]*$"
Where,
- ^ matches the starting of the sentence.
- [a-zA-z] matches the lower case and upper case letters.
- * indicates the occurrence for zero or more times.
- & indicates the end of the line.
Example 1
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ContainsAlphabetExample { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String names[] = new String[5]; for(int i=0; i<names.length;i++){ System.out.println("Enter your name: "); names[i] = sc.nextLine(); } //Regular expression to accept English alphabet String regex = "^[a-zA-Z]*$"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); for (String name : names) { //Creating a Matcher object Matcher matcher = pattern.matcher(name); if(matcher.matches()) { System.out.println(name+" is a valid name"); } else { System.out.println(name+" is not a valid name"); } } } }
Output
Enter your name: krishna Enter your name: kasyap Enter your name: maruthi# Enter your name: Sai_Ram Enter your name: Vani.Viswanath krishna is a valid name kasyap is a valid name maruthi# is not a valid name Sai_Ram is not a valid name Vani.Viswanath is not a valid name
Example 2
import java.util.Scanner; public class Just { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your name: "); String name = sc.nextLine(); String regex = "^[a-zA-Z]*$"; boolean result = name.matches(regex); if(result) { System.out.println("Given name is valid"); } else { System.out.println("Given name is not valid"); } } }
Output
Enter your name: vasu#dev Given name is not valid
- Related Articles
- Check if a string contains only alphabets in Java using Lambda expression
- What is the Python regular expression to check if a string is alphanumeric?
- Java Program to Check if a string contains a substring
- Check if a string contains a number using Java.
- Java Program to split a string using Regular Expression
- Java program to check if a string contains any special character
- Method to check if a String contains a sub string ignoring case in Java
- How to check if a string contains a specific sub string?
- Check if a string contains only alphabets in Java using Regex
- Java Program to check if the String contains only certain characters
- How to Split String in Java using Regular Expression?
- Is it possible to check if a String only contains ASCII in java?
- How to check if a String contains another String in a case insensitive manner in Java?
- Check if a string contains a sub-string in C++
- MySQL query to check if a string contains a word?

Advertisements