
- 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 regex program to verify whether a String contains at least one alphanumeric character.
Following regular expression matches a string that contains at least one alphanumeric characters −
"^.*[a-zA-Z0-9]+.*$";
Where,
^.* Matches the string starting with zero or more (any) characters.
[a-zA-Z0-9]+ Matches at least one alpha-numeric character.
.*$ Matches the string ending with zero or more (ant) characters.
Example 1
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Example { public static void main(String args[]) { //Reading String from user System.out.println("Enter a string"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression String regex = "^.*[a-zA-Z0-9]+.*$"; //Compiling the regular expression Pattern pattern = Pattern.compile(regex); //Retrieving the matcher object Matcher matcher = pattern.matcher(input); int count = 0; if(matcher.matches()) { System.out.println("Given string is valid"); } else { System.out.println("Given string is not valid"); } } }
Output 1
Enter a string ###test123$$$ Given string is valid
Output 2
Enter a string ####$$$$ Given string is not valid
Example 2
import java.util.Scanner; public class Example { public static void main(String args[]) { //Reading String from user System.out.println("Enter a string"); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); //Regular expression String regex = "^.*[a-zA-Z0-9]+.*$"; boolean result = input.matches(regex); if(result) { System.out.println("Valid match"); }else { System.out.println("In valid match"); } } }
Output 1
Enter a string ###test123$$$ Valid match
Output 2
Enter a string ####$$$$ In valid match
- Related Articles
- Program to find whether a string is alphanumeric.
- Python Program to accept string ending with alphanumeric character
- Java program to check if a string contains any special character
- Java regex program to split a string at every space and punctuation.
- Python program to find Least Frequent Character in a String
- Program to check whether every one has at least a friend or not in Python
- How to test if a Java String contains a case insensitive regex pattern
- Check if both halves of the string have at least one different character in Python
- Check if a string contains only alphabets in Java using Regex
- How to extract a group from a Java String that contains a Regex pattern
- How to match a character from given string including case using Java regex?
- C# program to check if a string contains any special character
- Python program to check if a string contains any unique character
- Java Program to check whether one String is a rotation of another.
- Python Program to check if String contains only Defined Characters using Regex

Advertisements