使用正则表达式将字符串中的某些字母移动到Python中一系列字符串中同一字符串中的新位置

发布于 2025-01-11 16:17:50 字数 742 浏览 0 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柳絮泡泡 2025-01-18 16:17:50

您可以重新排列字符串。

new_list = []
for word in data:
    arr = word.split('-')
    new_word = '%s-%s-%s-%s'% (arr[0], arr[2], arr[1], arr[3])
    new_list.append(new_word)



You can rearrange the strings.

new_list = []
for word in data:
    arr = word.split('-')
    new_word = '%s-%s-%s-%s'% (arr[0], arr[2], arr[1], arr[3])
    new_list.append(new_word)



意犹 2025-01-18 16:17:50

您可以将以下正则表达式的匹配项替换为捕获组 1 的内容:

(?<=^[A-Z]\d)(?=.*(-(?:EMA|SA|UMA))(?=-))|-(?:EMA|SA|UMA)(?=-)

演示

正则表达式可以分解如下。

(?<=^[A-Z]\d)        # current string position must be preceded by a capital
                     # letter followed by a digit at the start of the string
(?=                  # begin a positive lookahead
  .*                 # match >= 0 chars other than a line terminator
  (-(?:EMA|SA|UMA))  # match a hyphen followed by one of the three strings
                     # and save to capture group 1
  (?=-)              # the next char must be a hyphen
)                    # end positive lookahead
|                    # or
-(?:EMA|SA|UMA)      # match a hyphen followed by one of the three strings
(?=-)                # the next character must be a hyphen

(?=-) 是一个正向前瞻

显然,这可能不适用于 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:

(?<=^[A-Z]\d)(?=.*(-(?:EMA|SA|UMA))(?=-))|-(?:EMA|SA|UMA)(?=-)

Demo

The regular expression can be broken down as follows.

(?<=^[A-Z]\d)        # current string position must be preceded by a capital
                     # letter followed by a digit at the start of the string
(?=                  # begin a positive lookahead
  .*                 # match >= 0 chars other than a line terminator
  (-(?:EMA|SA|UMA))  # match a hyphen followed by one of the three strings
                     # and save to capture group 1
  (?=-)              # the next char must be a hyphen
)                    # end positive lookahead
|                    # or
-(?:EMA|SA|UMA)      # match a hyphen followed by one of the three strings
(?=-)                # the next character must be a hyphen

(?=-) 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文