Java Regular Expressions
gocourse.in Maintenance

We'll be back soon

Our CDN (cdn.gocourse.in) is currently unreachable. Some images, JavaScript, or CSS files may not load properly.

Estimated downtime: ~30 minutes

Java Regular Expressions

Harine

Java Regular Expressions

What is a Regular Expression in Java?

A Regular Expression (Regex) is a sequence of characters that defines a search pattern. It is widely used for text processing, including searching, validating, extracting, and replacing data within strings.

In Java, regular expressions are handled using the java.util.regex package, which provides powerful tools for pattern matching.

Java Regex Classes

The java.util.regex package includes three key classes:

  • Pattern → Compiles a regular expression into a reusable pattern
  • Matcher → Performs match operations on input text
  • PatternSyntaxException → Handles invalid regex patterns

Basic Example: Pattern Matching in Java

Example: Check if an Email Pattern Exists

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class RegexExample {

   public static void main(String[] args) {

      String text = "Contact us at support@example.com";

      // Regex pattern for a simple email

      String regex = "\\w+@\\w+\\.\\w+";

      Pattern pattern = Pattern.compile(regex);

      Matcher matcher = pattern.matcher(text);

      if (matcher.find()) {

         System.out.println("Email found: " + matcher.group());

      } else {

         System.out.println("No email found");

       }

     }

  }

Explanation

  • Pattern.compile() → Compiles the regex pattern
  • matcher() → Applies the pattern to the input string
  • find() → Checks if a match exists
  • group() → Retrieves the matched substring

Using Flags in Regex

Flags modify how pattern matching behaves.

Commonly Used Flags

Example: Case-Insensitive Search

import java.util.regex.*;

public class CaseInsensitiveDemo {

   public static void main(String[] args) {

      String input = "Java Programming";

      Pattern pattern = Pattern.compile("java", Pattern.CASE_INSENSITIVE);

      Matcher matcher = pattern.matcher(input);

      System.out.println("Match found: " + matcher.find());

    }

  }

Common Regex Patterns

Character Classes (Brackets)

Example: Extract Digits

import java.util.regex.*;

public class DigitExtractor {

   public static void main(String[] args) {

      String data = "Order ID: 4589";

      Pattern pattern = Pattern.compile("[0-9]+");

      Matcher matcher = pattern.matcher(data);

      while (matcher.find()) {

         System.out.println("Number found: " + matcher.group());

       }

     }

  }

Metacharacters in Regex

Metacharacters have special meanings in regex patterns:

Example: Validate a Phone Number

import java.util.regex.*;

public class PhoneValidator {

   public static void main(String[] args) {

      String phone = "9876543210";

      String regex = "^[0-9]{10}$";

      Pattern pattern = Pattern.compile(regex);

      Matcher matcher = pattern.matcher(phone);

      System.out.println("Valid phone number: " + matcher.matches());

    }

  }

Quantifiers in Regex

Quantifiers define how many times a pattern should occur.

Example: Matching Repeated Characters

import java.util.regex.*;

public class QuantifierExample {

public static void main(String[] args) {

  String input = "aaab";

  Pattern pattern = Pattern.compile("a+");

  Matcher matcher = pattern.matcher(input);

  while (matcher.find()) {

  System.out.println("Match: " + matcher.group());

  }

 }

}

Our website uses cookies to enhance your experience. Learn More
Accept !