RegexSafe() function for coldfusion

It will probably already be out there somewhere, but I couldn't find it: a function which makes a string regular-expression-safe to use.

[Skip skip delete remove etcetera]
Edit 15 nov. 2010: See Peter Boughton's comment underneath; the function I thought of could have been made a LOT simpler, so I am just posting his much appreciated code here:

<cffunction name="regExSafe" returntype="string" access="public" output="no">
<cfargument name="text" type="string" required="yes" />
<cfreturn rereplace(arguments.text, "(?=[\[\]\\^$.|?*+()])", "\", "all") />
</cffunction>

Thanks Peter!

Have tested this on ACF 8 and Railo 3.2.0.001.

del.icio.us Digg StumbleUpon Facebook Technorati Fav reddit Google Bookmarks
| Viewed 4611 times
  1. Peter Boughton

    #1 by Peter Boughton - November 15, 2010 at 12:30 PM

    Most of those backslashes are not required - character classes have different special characters - indeed several of the characters listed there are not special inside or outside of a character class.

    And you don't need the wrapping parentheses - you've always got \0 for the entire match, so no need to create a \1 for it.

    Also, a better tmp variable is chr(65535), since it's technically an invalid character and thus should never appear in input.

    But there's a simpler way to workaround that Railo bug, you can use a lookahead instead (to match the position before the character and thus not require even \0), which then lets you use just a single \ and it works.

    In summary, you can do this:
    <cfreturn rereplace( arguments.text , "(?=[\[\]\\^$.|?*+()])" , "\" , "all" ) />

    :)
  2. Paul Klinkenberg

    #2 by Paul Klinkenberg - November 15, 2010 at 1:51 PM

    Hi Peter, thanks a lot! I learned two new things from your comment, great!
    Please see the revised blog post: http://www.railodeveloper.com/post.cfm/regexsafe-function-for-coldfusion
  3. Ben Nadel

    #3 by Ben Nadel - November 15, 2010 at 4:53 PM

    @Peter,

    Dang, that's pretty clever. I never though of replacing a zero-length match with an actual character (I'm not even sure I would have thought that was possible). Very cool!
(will not be published)
Leave this field empty

ambiguous