Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2005

Tip: Looking for answers? Try searching our database.

Validating a regex replacement string

Thread view: 
Avshi - 18 May 2005 08:48 GMT
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()+"\"");
   }
}
Alan Moore - 18 May 2005 11:20 GMT
>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
[quoted text clipped - 38 lines]
>    }
>}

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.
Avshi - 18 May 2005 13:46 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.