- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to match any character using Java RegEx
The meta character “.” in java regular expression matches any character (single) it could be the alphabet, number or, any special character.
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 to match any character String regex = "."; //Compiling the regular expression Pattern pattern = Pattern.compile(regex); //Retrieving the matcher object Matcher matcher = pattern.matcher(input); int count = 0; while(matcher.find()) { count ++; } System.out.println("Given string contains "+count+" characters."); } }
Output
Enter a String hello how are you welcome to tutorialspoint Given string contains 42 characters.
You can match any 3 characters between a and b using the following regular expression −
a…b
Similarly the expression “.*” matches n number of characters.
Example 2
Following Java program reads 5 strings from the user and accepts those which starts with b, ends with a with any number of characters in between them.
import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexExample { public static void main( String args[] ) { String regex = "^b.*a$"; Scanner sc = new Scanner(System.in); System.out.println("Enter 5 input strings: "); String input[] = new String[5]; for (int i=0; i<5; i++) { input[i] = sc.nextLine(); } //Creating a Pattern object Pattern p = Pattern.compile(regex); for(int i=0; i<5;i++) { //Creating a Matcher object Matcher m = p.matcher(input[i]); if(m.find()) { System.out.println(input[i]+": accepted"); } else { System.out.println(input[i]+": not accepted"); } } } }
Output
Enter 5 input strings: barbara boolean baroda ram raju barbara: accepted boolean: not accepted baroda: accepted ram: not accepted raju: not accepted
- Related Articles
- How to match a non-word character using Java RegEx?
- How to match a character from given string including case using Java regex?
- How to match word characters using Java RegEx?
- How to match word boundaries using Java RegEx?
- How to match digits using Java Regular Expression (RegEx)
- How to match non-word boundaries using Java RegEx?
- How to match a range of characters using Java regex
- How to match a white space equivalent using Java RegEx?
- How to match non-digits using Java Regular Expression (RegEx)
- How to match end of the input using Java RegEx?
- How to match any one uppercase character in python using Regular Expression?
- How to match any non-digit character in Python using Regular Expression?
- How to match a fixed set of characters using Java RegEx
- How to match a non-white space equivalent using Java RegEx?
- How to match the beginning of the input using Java RegEx?

Advertisements