Jesus 'n Jim
A mainly PC (some Mac) site w/Software, Computer Repair Info, How-To's on Using Computers
Technical Support 1-360-521-2060 (my business line cell)

new feature for regex - selectable group alternation substitution

 

Overview

there is currently no way to do code substitutions in regular expressions. I mean there is no way to take a list of codes and substitute them with other codes or another pattern. for example, cat flat or fish replace with dog thin or flo\2un\1der.

the idea

maybe regex libraries need some sort of group substitution thing that can replace with a selection of patterns, like a (?;cat|dog|fish|bird) replacing with (;Car|SUV|Hatchback|Truck) by matching and replacing the 1st, 2nd, 3rd, or 4th item from both groups. that would make the library even more powerful. it would be nice of somebody added something like this to POSIX. some sort of alternation code replacement.

for example, cat would be replaced with Car and dog would be replaced with SUV, fish would be replaced with Hatchback, and bird would be replaced with Truck. you must have an equal number of matches and substitutions on both sides.

I don't think this can be done with \1..\9 AKA $1..$9 capture groups. those don't substitute the source group with something else (they simply copy).

this proposal would select from a | separated list of substitutes in (;...) to be chosen from based on which of the source items in the source list matches an selected pattern in (?;...). If you can think of a better way to implement this, by all means put it forth.

like the regular alternation operator, alternation is greedy. it gobbles as many characters around the | as possible.