Mark up patterns and escape all input?

I want to replace e.g.
“ABCX^HXY^HYZ^HZDEF” with “ABCXYZDEF”
“ABC_^HX_^HY_^HZDEF” with “ABCXYZDEF”
and “&<>&^H&<^H<>^H>^H&^H<_^H>” with
“&<>&<>&<>”

In other words I want to wrap sequences of any character followed by
backspace followed by the same character with , wrap sequences of
underscore followed by backspace followed by any character with , and
escape all of the input.

How would you implement this in Ruby?

Here’s how I implemented it in Python, but I’m struggling to port it
Ruby because Ruby’s String.split() works differently than Python’s
RegexObject.split()

import cgi, re

Find sequences of X^HX (there can be whitespace in the middle of the

sequence) or _^HX (there can be vertical whitespace or underscores

in the middle of the sequence) where X is any character and ^H is

backspace

parts = re.compile(’((.)\b\2(?:\s*(.)\b\3))|(\b.(?:[\n-\r]_\b.)*)’,
re.UNICODE).split(input)

Escape all parts of the input and wrap sequences of X^HX in and

sequences of _^HX in

parts[0::5] = map(cgi.escape, parts[0::5])
parts[1::5] = (group and ‘’ + cgi.escape(re.compile(’.\b’).sub(’’, group)) +
’ for group in parts[1::5])
parts[4::5] = (group and ‘’ + cgi.escape(group.replace(’_\b’, ‘’)) + ‘’
for group in parts[4::5])

print ‘’.join(parts[i] for i in range(len(parts)) if i % 5 in (0, 1, 4) and
parts[i])

Jack Bates wrote in post #1153000:

I want to replace e.g.
“ABCX^HXY^HYZ^HZDEF” with “ABCXYZDEF”
“ABC_^HX_^HY_^HZDEF” with “ABCXYZDEF”

Here, I replace ^H by ‘a’ :

a=“ABCXaXYaYZaZDEF”
class Array
def chunk_cons()
a=first
nos=0
each.chunk { |b|
r=yield(a,b); a=b; nos+=1 unless r ;nos}.
map {|(a,b)| b }
end
end

p a.split(/(.a.)/).select {|a|a.size>0}.chunk_cons {|a,b|
a+b=~/(.)a\1(.)a\2/}.
map {|l|
l.size==1 ? l.first : “#{l.join(’’).gsub(/a./,’’)}
}.join(’’)

Jack Bates wrote in post #1153000:

I want to replace e.g.
“ABCX^HXY^HYZ^HZDEF” with “ABCXYZDEF”
“ABC_^HX_^HY_^HZDEF” with “ABCXYZDEF”

a=“ABCXaXYaYZaZDAaAEF ABC_as_at_auDEF”
a=“ABCXaXYaYZaZDAaAEF ABC_as_at_auDEF”

p a.gsub(/(.)a\1/) {|m| “#{m[0]}”}.gsub(’’,"").
gsub(/_a./) {|m| “#{m[2]}”}.gsub(’’,"")

ABCXYZDAEF ABCstuDEF"

Thanks but I haven’t managed to implement this with String.gsub()

a = “&<>&a&<a<>a>_a&_a<_a>”
p a.gsub(/(.)a\1/) {|m| “#{m[0]}”}.gsub(’’,"").
gsub(/_a./) {|m| “#{m[2]}”}.gsub(’’,"")
=> “&<>&<>&<>”

Whereas I need “&<>&<>&<>”