PatternSplit.java Source code

Java tutorial

Introduction

Here is the source code for PatternSplit.java

Source

import java.util.regex.Pattern;

public class PatternSplit {
    public static void main(String args[]) {

        String statement = "I will not compromise. I will not "
                + "cooperate. There will be no concession, no conciliation, no "
                + "finding the middle ground, and no give and take.";

        String tokens[] = null;
        String splitPattern = "compromise|cooperate|concession|"
                + "conciliation|(finding the middle ground)|(give and take)";

        Pattern p = Pattern.compile(splitPattern);

        tokens = p.split(statement);

        System.out.println("REGEX PATTERN:\n" + splitPattern + "\n");

        System.out.println("STATEMENT:\n" + statement + "\n");

        System.out.println("TOKENS:");
        for (int i = 0; i < tokens.length; i++) {
            System.out.println(tokens[i]);
        }
    }
}