[thelist] RegEx Fun

Seth Bienek seth at sethbienek.com
Fri Jan 4 17:17:58 CST 2002

Hey Jeff,

>i need some assistance from those of you with more confidence with regular
>expressions than i have.
>let's use the following strings as the examples:
>in these strings, the "pageresponse" url variable could have any string of
>text after it, but will always be url encoded.  in the case of any of the
>strings above, i want to find instances of "&pageresponse=Item%20added",
>"pageresponse=Item%20updated", or "pageresponse=Item%20removed".  this is
>the first pattern i'm looking for.

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

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:]]*", "")>

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

You could do what you are proposing simply like this:

<cfset MyURL =

<cfset varsToDelete = "cfid,cftoken,pageresponse">

<cfloop list="#varsToDelete#" index="i">
	<cfset MyURL = ReReplaceNoCase(MyURL, "[?|&]#i#=[^[&|[:space:]]*", "")>


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

Hope it helps,


More information about the thelist mailing list