[thelist] RegEx Fun

.jeff jeff at members.evolt.org
Fri Jan 4 17:56:12 CST 2002


> From: Seth Bienek
> The same regexp will work for the last two data items
> you are wanting to extract (with the one obvious
> change. :)

somehow i felt that one regex would work for this, but wasn't quite sure how
to go about it.

> What you may consider doing is looking for the end of
> the name/value pairs, and excluding them that way. Since
> we know that URL name/value pairs always end with either
> the start of a new value (&) or the end of the URL
> itself (line breaks are considered to be in the same
> class of characters as spaces as far as CF's posix-based
> RegEx engine is concerned), you can eliminate any
> key/value pair like so:
> <cfset myNewVar = ReReplaceNoCase(searchstring,
> "[?|&]VarToReplace=[^[&|[:space:]]*", "")>

and bingo, that took care of it.

> Which says "If a string within the variable
> 'searchstring' starts with a question mark or ampersand,
> followed by the name of the variable I want to eliminate
> and the equal sign, then everything up until we hit the
> next ampersand or space-type character is a match" then
> replace it with an empty string.

ok, precisely where in the regex does it allow characters in between the "="
and the name/value delimiting character (& or [:space:])?  or does
"[^[&|[:space:]]" mean anything that's "NOT & or [:space:]"?  nevermind, i
think i figured it out myself.

> That would also make a nifty UDF if you're using CF5.

yes it would.

<tip type="ColdFusion" author=".jeff" coauthor="seth bienek">

  function URLReplace(string, substring1, substring2)
    var returnValue = string;
    var scope = 'one';
    if(ArrayLen(arguments) EQ 4) scope = arguments[4];
    returnValue = REReplaceNoCase(string, '[?|&]' & substring1 &
'=[^[&|[:space:]]*', substring2, scope);
    return returnValue;




jeff at members.evolt.org

More information about the thelist mailing list