/* * [TestRegexFindInsensitive.java] * * Summary: Finding with a Regex, case-insensitive. Prove \\p{lower] trumps CASE_INSENSITIVE. * * Copyright: (c) 2011-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2011-01-17 initial release */ package com.mindprod.example; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * Finding with a Regex, case-insensitive. Prove \\p{lower] trumps CASE_INSENSITIVE. *

* Use a regex to see repeated instances of a pattern embedded in a large string . * * @author Roedy Green, Canadian Mind Products * @version 1.0 2011-01-17 initial release * @since 2011-01-17 */ public class TestRegexFindInsensitive { /** * Regex pattern we look for embedded in big string. By default case-sensitive. */ private static final Pattern pattern = Pattern.compile( "\\p{Lower}+", Pattern.CASE_INSENSITIVE ); /** * test harness * * @param args not used */ public static void main( String[] args ) { final String lookIn = "apple pear 66 banana Orange LEMON pomegranATE "; final Matcher m = pattern.matcher( lookIn ); // Matchers are used both for matching and finding. while ( m.find() ) { final int gc = m.groupCount(); // group 0 is the whole pattern matched, // loops runs from from 0 to gc, not 0 to gc-1 as is traditional. for ( int i = 0; i <= gc; i++ ) { out.println( i + " : " + m.group( i ) ); } } // prints // 0 : apple // 0 : pear // 0 : banana // 0 : range // 0 : pomegran // in other words lower over-rides CASE_SENSITIVE. not upper case letters are matched } }