Validating a regex replacement string

  • Follow


I'm trying to validate a replacement string during construction time,
but the only way I found is really ugly. Please let me know if there's
a better way.

Thank you,
Avshi Avital

This is how I do it:


/**
 * Verifying the replacement string is a bit awkward,
 * because the only way to do this is to run the
 * replacement string on a text that the regex matches
 * and see if an exception is thrown.
 * We chit by counting the number of capturing groups
 * in the regex and then creating a fake regex that
 * contains that many capturing groups. Since we
 * construct the (fake) regex, we can also produce a
 * text that matches it.Running the replacement string
 * on that text tests its validity.
 */
private void verifyReplacementString(Pattern, pattern,
        String replacement)
{
    int groupCount = pattern.matcher("").groupCount();
    StringBuffer sb = new StringBuffer();
    sb.append(".*");
    for (int i = 0; i < groupCount; i++) {
        sb.append("()");
    }

    // This should throw if the replacement is invalid.
    try {
        Pattern.compile(
            sb.toString()).matcher("").replaceAll(replacement);
    } catch (IndexOutOfBoundsException e) {
        throw new IllegalStateException(
            "Replacement string \""+replacement +
            "\" is illegal for regex \"" +
            pattern.pattern()+"\"");
    }
}

0
Reply avshi.avital (3) 5/18/2005 7:48:03 AM

On 18 May 2005 00:48:03 -0700, "Avshi" <avshi.avital@gmail.com> wrote:

>I'm trying to validate a replacement string during construction time,
>but the only way I found is really ugly. Please let me know if there's
>a better way.
>
>Thank you,
>Avshi Avital
>
>This is how I do it:
>
>
>/**
> * Verifying the replacement string is a bit awkward,
> * because the only way to do this is to run the
> * replacement string on a text that the regex matches
> * and see if an exception is thrown.
> * We chit by counting the number of capturing groups
> * in the regex and then creating a fake regex that
> * contains that many capturing groups. Since we
> * construct the (fake) regex, we can also produce a
> * text that matches it.Running the replacement string
> * on that text tests its validity.
> */
>private void verifyReplacementString(Pattern, pattern,
>        String replacement)
>{
>    int groupCount = pattern.matcher("").groupCount();
>    StringBuffer sb = new StringBuffer();
>    sb.append(".*");
>    for (int i = 0; i < groupCount; i++) {
>        sb.append("()");
>    }
>
>    // This should throw if the replacement is invalid.
>    try {
>        Pattern.compile(
>            sb.toString()).matcher("").replaceAll(replacement);
>    } catch (IndexOutOfBoundsException e) {
>        throw new IllegalStateException(
>            "Replacement string \""+replacement +
>            "\" is illegal for regex \"" +
>            pattern.pattern()+"\"");
>    }
>}

The only other way I can see to do it is to parse the replacement
string looking for the largest $n group reference and compare it to
the groupCount() value.  I prefer your way.
0
Reply jbigboote1 (87) 5/18/2005 10:20:08 AM


Another reason that I did it the way I did is that while parsing the
replacement string you have to ignore escaped '$' and also pay
attention to escaped escapes ("\\")... It get's ugly.

0
Reply avshi.avital (3) 5/18/2005 12:46:22 PM

2 Replies
14 Views

(page loaded in 0.104 seconds)


Reply: