How do I use logical OR operator in regex?

You can use the | operator (logical OR) to match characters or expression of either the left or right of the | operator. For example the [t|T] will match either t or T from the input string.

package org.kodejava.example.util.regex;

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

public class LogicalOrRegexDemo {
    public static void main(String[] args) {
        //
        // Define regex that will search characters 't' or 'T'
        //
        String regex = "[t|T]";

        //
        // Compiles the pattern and obtains the matcher object.
        //
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(
                "The quick brown fox jumps over the lazy dog");

        //
        // Find every match and print it
        //
        while (matcher.find()) {
            System.out.format("Text "%s" found at %d to %d.%n",
                    matcher.group(), matcher.start(), matcher.end());
        }
    }
}

The program print the following result:

Text "T" found at 0 to 1.
Text "t" found at 31 to 32.

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA. Support me by donating >> here <<.

1 Comments

Leave a Reply