package com.mindprod.example;

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

/**
 * use a regex to check that an entire string matches a regex pattern.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.1, 2008-01-29
 */
public class TestMatchWithRegex
    {
// ------------------------------ FIELDS ------------------------------

    private static final Pattern p = Pattern.compile( "\\<td>([^\\<>]++)\\</td>" );

// --------------------------- main() method ---------------------------

    /**
     * test harness
     *
     * @param args not used
     */
    public static void main( String[] args )
        {
        // Test if entire string is a perfect match for the Pattern
        // will print:
        // 0 : <td>orca</td>
        // 1 : orca

        final String lookIn = "<td>orca</td>";

        final Matcher m = p.matcher( lookIn );
        if ( m.matches() )
            {
            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++ )
                {
                System.out.println( i + " : " + m.group( i ) );
                }
            }
        }
    }