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])