Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java RegEx

What is a Regular Expression?

A regular expression, comprised of characters, serves as a pattern for searching within text data, allowing you to specify the criteria for your search.

A regular expression can range from a single character to a more intricate pattern.

Regular expressions enable various types of text search and replace operations.

In Java, there isn’t a dedicated Regular Expression class, but we can utilize the java.util.regex package to handle regular expressions, which includes several classes.

  • The Pattern Class: defines a pattern for use in searches.
  • The Matcher Class: conducts the search based on the pattern.
  • The PatternSyntaxException Class: This class flags syntax errors within a regular expression pattern.

Example

Determine if the word “code7school” appears in a sentence.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public
class Main {
  public static void main(String[] args) {
    Pattern pattern = Pattern.compile("code7chool", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher("Visit W3Schools!");
    boolean matchFound = matcher.find();
    if(matchFound) {
      System.out.println("Match found");
    } else {
      System.out.println("Match not found");
    }
  }
}

// Outputs Match found

Example Explained

In this instance, the sentence is being scanned for the presence of the term “code7school”.

Initially, the pattern is established using the Pattern.compile() method. The first argument specifies the pattern sought, while the second argument, an optional flag, signifies a case-insensitive search.

The matcher() method is employed to seek the pattern within a string. It yields a Matcher object that encapsulates details regarding the executed search.

The find() method yields true if the pattern is discovered within the string, and false if it is not.

Flags

Flags passed into the compile() method modify the behavior of the search operation. Here are some examples:

  1. Pattern.CASE_INSENSITIVE: means that the search will ignore the case of letters.
  2. Pattern.LITERAL: indicates that special characters in the pattern will be treated as ordinary characters without any special meaning during a search.
  3. Pattern.UNICODE_CASE: When used alongside the CASE_INSENSITIVE flag, it enables the ignoring of letter case even for characters outside the English alphabet during a search.

Regular Expression Patterns

Brackets in the Pattern.compile() method’s first parameter denote a pattern, specifying what is sought during a search.

They are utilized to identify a range of characters.

Expression

Description

[abc]

Locate a single character from the choices listed within the brackets.

[^abc]

Locate a single character that is outside the range specified within the brackets.

[0-9]

Locate a single character within the numeric range from 0 to 9.

Metacharacters

Metacharacters are characters endowed with special significance.

Metacharacter

Description

|

Locate a match for any one of the specified patterns separated by |, such as: cat, dog, or fish.

.

Locate a single occurrence of any character.

^

Locates a match only at the beginning of a string, as in: ^Hello.

$

Locates a match at the end of a string, as in: World$.

\d

Locate a numeric digit.

\s

Locate a whitespace character.

\b

Match \bWORD at the beginning of a word, or WORD\b at the end of a word.

\uxxxx

Locate the Unicode character represented by the hexadecimal number xxxx.

Quantifiers

Quantifiers specify quantities.

Quantifier

Description

n+

Matches any string containing at least one instance of the character “n”.

n*

Matches any string containing zero or more occurrences of the character “n”.

n?

Matches any string containing zero or one occurrence of the character “n”.

n{x}

Matches any string containing a sequence of “X” occurrences of the character “n”.

n{x,y}

Matches any string containing a sequence of “X” to “Y” occurrences of the character “n”.

n{x,}

Matches any string containing a sequence of at least “X” occurrences of the character “n”.