Meta characters in Java Regex


Regex stands for Regular Expression. A pattern may be defined using regex as an API to search or modify the Text. It is frequently employed to specify string constraints, including passwords, and email validation. Once you understand this term, you might use regex to evaluate your regular expressions.

Java Regex provides three classes and one interface in java.util.regex package namely, MatchResult Interface, Matcher class, Pattern class, PatternSyntaxException class. The Java regex feature is offered via the matcher and pattern classes.

Meta Characters in Java Regex

Meta Characters in Java Regex works as a short code for common matching patterns. Meta character precede with backlash (\). All the meta characters are listed below:

REGEX

DESCRIPTION

.

Any character (may or may not match terminator)

\d

Any digit, short for [0-9]

\D

Any non-digit, short for [^0-9]

\s

Any white space character, short for [\t\n\x0B\f\r]

\S

Any non-white space character, short for [^\s]

\w

Any word character, short for [a-zA-Z_0-9]

\W

Any non-word character, short for [^\w]

\b

A word boundary

\B

A non-word boundary

Example of Meta Characters

// Java program to demonstrate all the meta characters in Java Regex
import java.io.*;
import java.util.regex.*;
public class example {
   public static void main(String[] args) {
      // \d- Number
      // \D- Non-Digit
      // \s- Any whitespace
      // \S- Non whitespace Character
      // \w- Any word character like numbers/ characters
      // \W- Special symbols
      System.out.println(Pattern.matches(
         "\d\D\s\S\w\W", "1G FG!"));    // true
      System.out.println(Pattern.matches(
         "\d\D\s\S\w\W", "Hello"));     // false
   }
}

Output

true
false

Explanation of Meta Characters

Digit and Non-Digit Meta Characters (\d, \D)

Example

// Java program to demonstrate the Digit and Non-Digit related Meta Characters

import java.io.*;
import java.util.regex.*;
public class example {
   Public static void main(String[] args) {
      // \d represents a digit
      // represents a number so return true
      System.out.println(Pattern.matches("\d", "2"));  // true

      // Comparing a number with character so return false
      System.out.println(Pattern.matches("\D", "a"));   // false

      // \D represents non digits
      // Comparing a non-digit with character so return
      // true
      System.out.println(Pattern.matches("\D", "a"));  // true

      // Comparing a non-digit with a digit so return
      // false
      System.out.println(Pattern.matches("\D", "2"));  // false

   }
}

Output

true
false
true
false

Explanation

d meta character represents a digit from 0 to 9. So, when we compare "d" within the range, then it returns true. Else return false.

D meta character represents a non-digit that accepts anything except numbers. So, when we compareD" with any number, then it returns false. Else true.

Whitespace and Non whitespace Meta Characters (\s, \S)

Example

// Java program to demonstrate the Whitespace and Non whitespace Meta Characters
import java.io.*;
import java.util.regex.*;
Class example {
   public static void main(String[] args) {
      // comparing any whitespace character with a white
      // space so return else false
      System.out.println(Pattern.matches("\s", " "));   // true
      System.out.println(Pattern.matches("\s", "2"));   // false
 
      // comparing any non-whitespace character with a non
      // white space character so return true else false
      System.out.println(Pattern.matches("\S", "2"));   // true
      System.out.println(Pattern.matches("\S", " "));   // false
   }
}

Output

true 
false
true
false

Explanation

s represents whitespace characters like space, tab space, new line etc. So, when we compare "s" with whitespace characters, then it will return true. Else false

S represents non-whitespace characters that accept everything except whitespace, so when we compare "S" with whitespace characters, then it will return false. Else true.

Word and Nonword Meta Characters (\w, \W)

Example

// Java program to demonstrate the Word and Non-Word Meta Characters
import java.io.*;
import java.util.regex.*;
public class example {
   public static void main(String[] args){
      // comparing any word character with a word
      // character so return true else false
      System.out.println(Pattern.matches("\w", "a"));  // true
      System.out.println(Pattern.matches("\w", "2"));  // true
      System.out.println(Pattern.matches("\w", "$"));  // false

      // comparing any non-word character with special
      // symbols and whitespaces return true else false
      System.out.println(Pattern.matches("\W", "2"));  // false
      System.out.println(Pattern.matches("\W", " "));  // true
      System.out.println(Pattern.matches("\W", "$"));  // true
   }
}

Output

true
true
false
false
true
true

Explanation

w represents a word character which accepts alphabets (Capital and Small) and digits [0-9]. So, when we compare "w" with an alphabet or number it returns true. Else false.

W represents a non-word character which accepts anything except alphabets and digits. So, when we compare "W" with an alphabet or number then it returns false. Else true.

Word and Non-Word Meta Characters (\b, \B)

Example

// Java program to demonstrate the Word and Non Word boundary Meta Characters
import java.io.*;
import java.util.regex.*;
class example {
   public static void main(String[] args) {
      // \b says that a string must have boundary letters
      // of word characters
      System.out.println(Pattern.matches("\bexample\b", "example")); // true
      System.out.println(Pattern.matches("\b@example\b", "@example")); // false
  
      // \B says that a string must have non- word characters as boundaries
      System.out.println(Pattern.matches("\B@example@\B", "@example@")); //true
      System.out.println(Pattern.matches("\Bexample\B", "example"));  //false

   }
}

Output

true
false
true
false

Explanation

A string must have boundary elements of word characters, such as numbers or alphabets, according to the symbol b. Since the GFG string in this case includes limits that are word letters (G, G), it returns true. The boundary elements for the @GFG string are @, G, where @ is not a word character, hence return false.

B means a string's boundary components must be non-word characters; anything other than numerals or alphabets is permitted. Since the bounds of the @GFG@ string are @, @ non-word characters, it returns true. The boundary elements for the GFG string are G and G, which are word characters and provide a false result.

Conclusion

For programmers seeking to detect or manipulate textual information, regular expressions (often called regex) offer an efficient solution. By defining patterns in strings, developers can easily locate desired content and even modify its appearance. Java Regex classes are present in java.util.regex package. It needs to be imported before using any of the methods of Java Regex. Java Regex contains three classes and one interface namely, pattern, matcher, PatternSyntaxException and MatchResult Interface.

Updated on: 01-Aug-2023

277 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements