- 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 non-digits using Java Regular Expression (RegEx)
You can match non-digit character using the meta character "\D".
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(); String regex = "\D"; //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("Number non-digit characters: "+count); } }
Output
Enter a String sample text 2425 36 Number non-digit characters: 13
Example 2
import java.util.Scanner; public class RegexExample { public static void main( String args[] ) { //regular expression to accept 5 letter word String regex = "\D{10}"; System.out.println("Enter input value: "); Scanner sc = new Scanner(System.in); String input = sc.nextLine(); boolean result = input.matches(regex); if(result) { System.out.println("input matched"); } else { System.out.println("wrong input"); } } }
Output 1
Enter input value: sample abc input matched
Output 2
Enter input value: sample1234 wrong input
- Related Articles
- How to match digits using Java Regular Expression (RegEx)
- How to match only digits in Python using Regular Expression?
- How to match non-word boundaries using Java RegEx?
- How to match only non-digits in Python using Regular \nExpression?\n
- How to remove white spaces using Java Regular Expression (RegEx)
- How to match a non-word character using Java RegEx?
- How to match a non-white space equivalent using Java RegEx?
- How to match any non-digit character in Python using Regular Expression?\n\n
- How to match n number of occurrences of an expression using Java RegEx?
- How to use regular expression in Java to pattern match?
- PHP – Match regular expression using mb_ereg_match()
- How to match a word in python using Regular Expression?
- How to match a whitespace in python using Regular Expression?
- How to match any character using Java RegEx
- How to match word characters using Java RegEx?

Advertisements