RegEx Backreferences

Backreferences are cool, but I've never needed to use them before.

I just ran into something where they were quite handy.

I had to manipulate a string like the following:

TNF S/S VENT POLY SHIRT W

The end goal was to see if the string had either a S/S or L/S in it, along with SHIRT. If both substrings were found, remove SHIRT and move the S/S or L/S to the end of the string.

It's not hard, but a one liner kind of is. Backreferences really helped out here.

#rereplace("TNF S/S VENT POLY SHIRT W","(.*)([LS]/S)(.*)SHIRT(.*)","\1\3\4 \2")#

When you enclose the expression in parenthesis, you can reference that using a backreference. Pretty handy.

The string that results from the above code is:

TNF VENT POLY W S/S

I know my regex syntax is weak but hey, it worked.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)

Psykel blog uses BlogCFC (by Raymond Camden). Layout design inspired by arcsin