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"));
    }
}


Output

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:

Similar Reads

Regex Classes and Interfaces

Regex in Java provides 3 classes and 1 interface which are as follows:...

Pattern Class

This class is a compilation of regular expressions that can be used to define various types of patterns, providing no public constructors. This can be created by invoking the compile() method which accepts a regular expression as the first argument, thus returning a pattern after execution....

Matcher class

...

Regex Character classes

This object is used to perform match operations for an input string in Java, thus interpreting the previously explained patterns. This too defines no public constructors. This can be implemented by invoking a matcher() on any pattern object....

Regex Metacharacters

...

Java Regex Finder Example

Character Class                     Description [xyz] x,y or z [^xyz] Any characters other than x,y or z [a-zA-Z] characters from range a to z or A to Z. [a-f[m-t]] Union of a to f and m to t. [a-z && p-y]  All the range of elements intersection between two ranges  [a-z && [^bc]] a to z union with except b and c [a-z && [^m-p]] a to z union with except range m to p...

Conclusion

...

FAQs in Java Regex

Regex                      Description X? X appears once or not X+ X appears once or more than once X* X appears zero or not once X{n} X appears n times X{n,} X appears n times or more than n X{n,m} X appears greater than equal to n times and less than m times....