/* * [TestRegexLookingAt.java] * * Summary: LookingAt with a Regex. * * Copyright: (c) 2009-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 2008-01-29 */ package com.mindprod.example; import java.util.regex.Matcher; import java.util.regex.Pattern; import static java.lang.System.*; /** * LookingAt with a Regex. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2008-01-29 * @since 2008-01-29 */ public class TestRegexLookingAt { private static final Pattern pattern = Pattern.compile( "([^<>]++)" ); /** * main method * * @param args not used */ public static void main( String[] args ) { // Test if string starts with a match for the Pattern // will print: // 0 : orca // 1 : orca final String lookIn = "orca then some unrelated junk"; final Matcher m = pattern.matcher( lookIn ); if ( m.lookingAt() ) { 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 ) ); } } } }