How to check Regular expression in java Program

import java.util.regex.*;

public class RegexExample1
{
    public static void main(String args[])
    {
        // 1st way
        Pattern p = Pattern.compile(".s"); // . represents single character
        Matcher m = p.matcher("as");
        boolean b = m.matches();

        // 2nd way
        boolean b2 = Pattern.compile(".s").matcher("as").matches();

        // 3rd way
        boolean b3 = Pattern.matches(".s", "as");

        System.out.println(b + " " + b2 + " " + b3);
    }
}

Step-by-Step Explanation

1. Importing the java.util.regex Package

import java.util.regex.*;

This program imports all classes from the java.util.regex package. The classes in this package are Pattern and Matcher, which enable matching strings according to regular expressions.

2. Class Declaration:

public class RegexExample1

A class named RegexExample1 is declared, and the main method is defined within it.

3. Main Method:

public static void main(String args[]) {

The main method is the entry point of the program. It will execute the logic defined inside it.

4. 1st Way to Use Regular Expressions:

Pattern p = Pattern.compile(\”.s\”);

Matcher m = p.matcher(\”as\”);

boolean b = m.matches();

  • Pattern.compile(\”.s\”) compiles a regex pattern (.s). By this pattern “.s”, meaning any single character (.) is followed by ‘s’.
  • The matcher(“as”) returns Matcher object to actually match the “as” to the pattern so compiled.
  • The m.matches() method checks whether the whole string  “as”  matches the pattern .s. Since “as”  does match this pattern (any one character followed by s), the result will be true.
  • The value true is assigned to the variable b.

5. Using Alternating Form of Regex Patterns:

boolean b2 = Pattern.compile(“.s “).matcher(” as “).matches();

This is more concise than the first method. It compiles both pattern and matcher object in one line.

  • Pattern.compile(\\\”.s\\\”) compile the regex pattern.s.
  • matcher(\\\”as\\”) will generate Matcher object for string  “as”.
  • matches() will check the string is matching the specified pattern and returns true since the string  “as” is matched by the regex pattern.
  • The boolean variable b2 is assigned to the value output from the following statement.

6. 3rd Method by using Regular Expression:

boolean b3 = Pattern.matches(“.s”, “as”);

  • This is the very basic way that one can have for determining a string to see whether it follows a pattern which is provided in the form of regex in Java.
  • Pattern.matches(.s, as) directly searches for a string  as  whether this matches the.s pattern.
  • Since as  fits the pattern-that is, any character followed by s-it becomes true and the result is stored in the variable b3.

7. Outputting the Results:

System.out.println(b + ” ” + b2 + ” ” + b3);

Finally, the program prints the values of b, b2, and b3. All three methods have found a successful match for the pattern.s with the string “as”, so all three values will be true.

The output will be:

true true true

Conclusion of the Program

  • The program demonstrates three different techniques for matching the regular expression against the string in Java.
  • It uses the pattern.s, meaning any character followed by the letter s.
  • It tests the string “as” against the pattern using all three methods: once with the Pattern and Matcher objects, and again just with the easier Pattern.matches() method.

Output:

true true true

Naturally, the above output happens because of the presence of the word “as” that matches against the pattern in all of the above scenarios.

Leave a Comment

Your email address will not be published. Required fields are marked *