package com.mindprod.example;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.System.*;
/**
* Finding a quoted String with a regex.
* <p/>
* This program is based on newsgroup posts by markspace (aka Brendan), Lew and Robert Klemme
* in response to my query about the cleanest way to use a regex to find quoted Strings.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.1 2012-05-26 make program verify its own results.
* @since 2012-05-25
*/
public class TestRegexFindQuotedString
{
/**
* test string to search alternating with what Patter should extract
*/
private static final String[] alternatingTestExpectedPairs =
{
"basic: href=\"http://mindprod.com\" ",
"\"http://mindprod.com\"",
"Nested quote: George said \"that's the ticket\".",
"\"that's the ticket\"",
"Nested tick: Jeb replied '\"ticket?\"what ticket'.",
"'\"ticket?\"what ticket'",
"Non-ASCII: \"How na\u00efve!\".",
"\"How na\u00efve!\"",
"empty: \"\"xx",
"\"\"",
"\\ escaped: 'Bob\\'s your uncle.'",
"'Bob\\'s your uncle.'",
"unbalanced (should fail): 'wonky\"",
"",
};
/**
* exercise a pattern to see if it finds the expected quoted string.
*/
private static void exercisePattern( Pattern pattern )
{
out.println();
out.println( "Pattern: " + pattern.toString() );
for ( int i = 0; i < alternatingTestExpectedPairs.length; i += 2 )
{
final String test = alternatingTestExpectedPairs[ i ];
final String expected = alternatingTestExpectedPairs[ i + 1 ];
final Matcher m = pattern.matcher( test );
boolean found = m.find();
final boolean correct;
final String extracted;
if ( found )
{
extracted = m.group( 0 );
correct = extracted.equals( expected );
}
else
{
extracted = null;
correct = false;
}
out.println( test + ", found: " + found +
", correct: " + correct + " (" + extracted + ")" );
}
}
/**
* test harness to exercise various candidate Patterns for finding quoted Strings.
*
* @param args not used
*/
public static void main( String[] args )
{
exercisePattern( Pattern.compile( "[\"']\\p{Print}+?[\"']" ) );
exercisePattern( Pattern.compile( "[\"'][^\"']+[\"']" ) );
exercisePattern( Pattern.compile( "([\"'])[^\"']+\\1" ) );
exercisePattern( Pattern.compile( "\"[^\"]+\"|'[^']+'" ) );
exercisePattern( Pattern.compile( "\"[^\"]*\"|'[^']*'" ) );
exercisePattern( Pattern.compile( "\"(?:\\\\.|[^\\\"])*\"|'(?:\\\\.|[^\\'])*'" ) );
}
}