Using parentheses to group characters - Java Regular Expressions

Java examples for Regular Expressions:Pattern

Introduction

You can use parentheses to create groups of characters to apply other regex elements to. For example:

The parentheses treat bla as a group, so the + quantifier applies to the entire sequence.

Demo Code

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

public class Main {
  public static void main(String[] args) {
    Pattern pattern = Pattern.compile("(bla)+");
    Matcher matcher = pattern.matcher("blablabla");
    if (matcher.matches())
      System.out.println("Match.");
    else//w  w w  .j ava2s.  c  o m
      System.out.println("Does not match.");
  }

}

Related Tutorials