Java-Alternation

Alternation

The vertical bar ( | ) operator denotes the logical OR operation, also called alternation or choice. The | operator does not operate on individual characters but instead applies to everything on either side of it. A regular expression to find “JAVA”,“PHP”, or “HTML” could be written as “JAVA|PHP|HTML”.

Program



Program Source

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

public class Javaapp {
  
    public static void main(String[] args) {
        
        Pattern pat = Pattern.compile("JAVA|PHP|HTML");
        Matcher mat = pat.matcher("JAVARUBYHTMLC++PHP");
        
        int i = 0;
        while(mat.find())
        {
            i++;
            System.out.println(i+"th subsequence  : "+mat.group());
        }
    }
}

Leave a Comment