/home/wpollock1/public_html/Java/RegexDemo.java

// Demo showing several Java regular expression processing features.
// This code shows using a case-insensitive substring match (using capturing
// groups), then replacing the specific matched group with something.
//
// Written 10/2017 by Wayne Pollock, Tampa Florida USA

import java.util.regex.*;

class RegexDemo {
    public static void main ( String [] args ) {
        StringBuilder sb = new StringBuilder( "blah FOO blah" );
        Matcher m =
        Pattern.compile( "(f(.)\\2)", Pattern.CASE_INSENSITIVE ).matcher( sb );
        if ( m.find() ) {  // looking for "foo"
           int start = m.start( 1 );
           int end = m.end( 1 );
           sb.replace(start, end, "BAR");
        } else {
            System.out.println( "No match!" );
        }
        System.out.println( sb.toString() );
    }
}