0

Given the following list or tuple:

list: [UURRUULLLRRDDDBBBUUU]

Step 1: Count how many times an "U" character repeats before a new "unknown2 (R or D or B or L?)" character appears and records (number of repetitions + letter of the respective "U" character)

Step2: Continued from Step 1: Count how many times the "unknown2" character repeats until a new character other than "unknown2" appears. or equal to the "U" character and record (number of repetitions + letter of the respective "unknown2" character).

Intermediate Step: If the new character after the "unknown2" character equals the "U" character count how many times it repeats in this new occurrence and repeat Step1.

That is, for the tuple (list) the result of the organized repetitions of the way I want is:

2U2R2U3L2R3D3B3U

Something similar would be This (but not in regex) besides the result is not the same but it is a start for me.

7beggars_nnnnm
  • 697
  • 3
  • 12
  • 1
    Regular expressions do not perform addition or subtraction or count totals. They match patterns. You can ask if a string contains a sequence of 2 or more `U` characters, but you can't count how many there are in that sequence unless you specify it yourself in the regex as a match pattern. – Ken White Jan 02 '20 at 01:13
  • 1
    `re.sub(r"(.)\1*", lambda x: "{}{}".format(len(x.group()), x.group(1)), text)`, [demo](https://ideone.com/aRbM7I) – Wiktor Stribiżew Jan 02 '20 at 01:24
  • ThankX @WiktorStribiżew worked for me but I will try to understand better. – 7beggars_nnnnm Jan 02 '20 at 01:59
  • 1
    @WiktorStribiżew For me it's resolved, I hope you post as final answer. – 7beggars_nnnnm Jan 02 '20 at 02:20

1 Answers1

1

You may use

re.sub(r"(.)\1*", lambda x: "{}{}".format(len(x.group()), x.group(1)), text)

See the online demo

Details

  • (.)\1* is a pattern that matches and captures a char (other than a line break char, use re.DOTALL to also match line breaks) and then matches 0+ same chars as in Group 1.
  • lambda x: "{}{}".format(len(x.group()), x.group(1)) is the replacement where x is the found match object, and the return value is a concatenation of the length of the whole match (len(x.group())) and the char in Group 1 (x.group(1)).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563