Java Regex Finder Example
Regex |
Description |
---|---|
. |
Any character |
\d |
Any digits, [0-9] |
\D |
Any non-digit, [^0-9] |
\s |
Whitespace character, [\t\n\x0B\f\r] |
\S |
Non-whitespace character, [^\s] |
\w |
Word character, [a-zA-Z_0-9] |
\W |
Non-word character, [^\w] |
\b |
Word boundary |
\B |
Non -Word boundary |
Below is the implementation of the Java Regex Finder:
Java
// Java Program to implement regex import java.io.*; import java.util.regex.*; // Driver Class class GFG { // Main Function public static void main(String[] args) { // Check if all elements are numbers System.out.println(Pattern.matches( "\\d+" , "1234" )); // Check if all elements are non-numbers System.out.println(Pattern.matches( "\\D+" , "1234" )); // Check if all the elements are non-numbers System.out.println(Pattern.matches( "\\D+" , "Gfg" )); // Check if all the elements are non-spaces System.out.println(Pattern.matches( "\\S+" , "gfg" )); } } |
true false true true
Regular Expressions in Java
In Java, Regular Expressions or Regex (in short) in Java is an API for defining String patterns that can be used for searching, manipulating, and editing a string in Java. Email validation and passwords are a few areas of strings where Regex is widely used to define the constraints. Regular Expressions in Java are provided under java.util.regex package. This consists of 3 classes and 1 interface. The java.util.regex package primarily consists of the following three classes as depicted below in tabular format as follows: