使用正则表达式将字符串中的某些字母移动到Python中一系列字符串中同一字符串中的新位置
我有一个包含 4000 个字符串的列表。每个字符串的命名约定都需要更改,我不想单独检查和编辑每个字符串。
该列表如下所示:
data = list()
data = ['V2-FG2110-EMA-COMPRESSION',
'V2-FG2110-SA-COMPRESSION',
'V2-FG2110-UMA-COMPRESSION',
'V2-FG2120-EMA-DISTRIBUTION',
'V2-FG2120-SA-DISTRIBUTION',
'V2-FG2120-UMA-DISTRIBUTION',
'V2-FG2140-EMA-HEATING',
'V2-FG2140-SA-HEATING',
'V2-FG2140-UMA-HEATING',
'V2-FG2150-EMA-COOLING',
'V2-FG2150-SA-COOLING',
'V2-FG2150-UMA-COOLING',
'V2-FG2160-EMA-TEMPERATURE CONTROL']
我需要将所有“SA”、“UMA”和“EMA”移至-FG 之前。
所需的输出是:
V2-EMA-FG2110-Compression
V2-SA-FG2110-Compression
V2-UMA-FG2110-Compression
...
V2-FG2 在整个列表中不会改变,所以我从那里开始,尝试了 re.sub 和 re.search,但我对 python 还很陌生,所以我得到了一堆不同的结果。任何帮助表示赞赏。
I have a list of 4000 strings. The naming convention needs to be changed for each string and I do not want to go through and edit each one individually.
The list looks like this:
data = list()
data = ['V2-FG2110-EMA-COMPRESSION',
'V2-FG2110-SA-COMPRESSION',
'V2-FG2110-UMA-COMPRESSION',
'V2-FG2120-EMA-DISTRIBUTION',
'V2-FG2120-SA-DISTRIBUTION',
'V2-FG2120-UMA-DISTRIBUTION',
'V2-FG2140-EMA-HEATING',
'V2-FG2140-SA-HEATING',
'V2-FG2140-UMA-HEATING',
'V2-FG2150-EMA-COOLING',
'V2-FG2150-SA-COOLING',
'V2-FG2150-UMA-COOLING',
'V2-FG2160-EMA-TEMPERATURE CONTROL']
I need all each 'SA' 'UMA' and 'EMA' to be moved to before the -FG.
Desired output is:
V2-EMA-FG2110-Compression
V2-SA-FG2110-Compression
V2-UMA-FG2110-Compression
...
The V2-FG2 does not change throughout the list so I have started there and I tried re.sub and re.search but I am pretty new to python so I have gotten a mess of different results. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以重新排列字符串。
You can rearrange the strings.
您可以将以下正则表达式的匹配项替换为捕获组 1 的内容:
演示
正则表达式可以分解如下。
(?=-)
是一个正向前瞻。显然,这可能不适用于 3.5 之前的 Python 版本,因为交替第二部分中的匹配不会为捕获组 1 分配值: “在 Python 3.5 之前,Python
re.sub
中对失败捕获组的反向引用未填充为空字符串。。@WiktorStribiżew 在链接中的回答。值得一提的是,我确认 Ruby 具有相同的行为 (
"V2-FG2110-EMA-COMPRESSION".gsub(rgx,'\1') #=> "V2-EMA-FG2110-COMPRESSION")。
当然,我们可以替换
(?<=^[AZ]\d)(-[AZ]{2}\d{4})(-(?:EMA|SA|UMA))( ?=-))
与$2 + $1
。即使不那么有趣,这可能更明智。You can replace matches of the following regular expression with the contents of capture group 1:
Demo
The regular expression can be broken down as follows.
(?=-)
is a positive lookahead.Evidently this may not work for versions of Python prior to 3.5, because the match in the second part of the alternation does not assign a value to capture group 1: "Before Python 3.5, backreferences to failed capture groups in Python
re.sub
were not populated with an empty string.. This quote is from@WiktorStribiżew 's answer at the link. For what it's worth I confirmed that Ruby has the same behaviour (
"V2-FG2110-EMA-COMPRESSION".gsub(rgx,'\1') #=> "V2-EMA-FG2110-COMPRESSION"
).One could of course instead replace matches of
(?<=^[A-Z]\d)(-[A-Z]{2}\d{4})(-(?:EMA|SA|UMA))(?=-))
with$2 + $1
. That's probably more sensible even if it's less interesting.