Matches one or more occurrences of the preceding character or group (+)

1. Match One or More Occurrences Using +

This program demonstrates the + quantifier in Java regular expressions. The + matches one or more consecutive occurrences of the preceding character or group. Unlike *, it does not match when the character occurs zero times.

import java.util.regex.*;

public class OneOrMoreMatch {
    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("Matched: " + matcher.group());
        }
    }
}
Output:
Matched: aaa

2. Match Repeated Characters Using +

This program uses + to find one or more consecutive occurrences of the character b. It matches groups containing one or more b characters. It does not produce an empty match when b is absent.

import java.util.regex.*;

public class RepeatedCharacterPlus {
    public static void main(String[] args) {
        String input = "abbcbb";
        Pattern pattern = Pattern.compile("b+");
        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            System.out.println("Matched: " + matcher.group());
        }
    }
}
Output:
Matched: bb
Matched: bb
Scroll to Top