在Regex或Python中:计数,添加和写入(重复次数+字符名称)并继续...,并在出现新情况时重复该过程 [英] In Regex or Python: Count, add and write (Number of times it repeats + character name) and proceed ... and in a new occurrence repeat the process

查看:44
本文介绍了在Regex或Python中:计数,添加和写入(重复次数+字符名称)并继续...,并在出现新情况时重复该过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下列表或元组:

列表:[UURRUULLLRRDDDBBBUUU]

list: [UURRUULLLRRDDDBBBUUU]

步骤1:计算在出现新的"unknown2(R或D或B或L?)"字符之前,"U"字符重复多少次并记录(重复次数+相应"U"字符的字母)

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)

步骤2:从步骤1继续:计算"unknown2"字符重复多少次,直到出现"unknown2"以外的新字符.或等于"U"字符并记录(重复次数+相应"unknown2"字符的字母).

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

中间步骤:如果在"unknown2"字符之后的新字符等于"U"字符,则在此新出现的次数中它重复多少次,然后重复步骤1.

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

2U2R2U3L2R3D3B3U

类似的东西是(但不是在正则表达式中),结果不一样,但这对我来说是一个开始.

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

推荐答案

您可以使用

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

请参见在线演示

详细信息

  • (.)\ 1 * 是一种匹配并捕获char的模式(除换行符char外,请使用 re.DOTALL 也匹配换行符)然后匹配与第1组相同的0+个字符.
  • lambda x:"{} {}".format(len(x.group()),x.group(1))是其中的替代品,其中 x 是找到的匹配对象,返回值是整个匹配长度( len(x.group()))和第1组中的char( x.group(1)).
  • (.)\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)).

这篇关于在Regex或Python中:计数,添加和写入(重复次数+字符名称)并继续...,并在出现新情况时重复该过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆