[Looking for Charlie's main web site?]

Using a range of hex values in a CF regular expression

If you were asked by someone to strip a string of all characters having ASCII codes from some value and higher, how would you do it? If you'd think to use a regular expression, you get 5 points. :-) But how would you get it to take a range? If you're ever stumped, here's how.

Assume you have a str1 variable with the string to be processed, and you want a resulting str2 holding the result. Let's assume as well that you've been asked to strip any codes with ASCII values of 160 or higher. This will do it:

<cfset str2 = rereplace(str1,"[\xa0-\xff]","","all")>

The CF docs do discuss using the \xnn option to search for a hex value, but they don't show how to specify a range. You might, as I did, try a few variations until you stumble upon it. That's the format.

Oh, and as for the xa0, that's the hex equivalent of decimal 160, and ff is the top of the range. I didn't find that it performed any more slowly with ff or some lower number.

Hope all that's helpful to someone. As for why you may do the above, I'd rather take that up in a separate entry which I hope to write today.

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.005.

Managed Hosting Services provided by
Managed Dedicated Hosting