gpt4 book ai didi

Python复杂的正则表达式字符串扩展

转载 作者:行者123 更新时间:2023-11-28 18:46:01 24 4
gpt4 key购买 nike

<分区>

假设我有一个如下形式的字符串:

ABCDEF_(0-100;1)(A|B)_GHIJ_(A-F)

我希望能够将其扩展到:

ABCDEF_0A_GHIJ_A
ABCDEF_1A_GHIJ_A
ABCDEF_2A_GHIJ_A
...
ABCDEF_100A_GHIJ_A

ABCDEF_0B_GHIJ_A
ABCDEF_1B_GHIJ_A
ABCDEF_2B_GHIJ_A
...
ABCDEF_100B_GHIJ_A

ABCDEF_0A_GHIJ_B
ABCDEF_1A_GHIJ_B
ABCDEF_2A_GHIJ_B
...
ABCDEF_100A_GHIJ_B

ABCDEF_0B_GHIJ_B
ABCDEF_1B_GHIJ_B
ABCDEF_2B_GHIJ_B
...
ABCDEF_100B_GHIJ_B

ABCDEF_0A_GHIJ_C
ABCDEF_1A_GHIJ_C
ABCDEF_2A_GHIJ_C
...
ABCDEF_100A_GHIJ_C

..and so on

第二行的字符串是:

STRING_(START-END;INC)_STRING(A OR B)_STRING(A THRU F)

但是,正则表达式符号可以在字符串中的任何位置。即字符串也可以是:

ABCDEF_(A|B)_(0-100;1)_(A-F)_GHIJ

到目前为止,这是我尝试过的:

trend = 'ABCDEF_(0-100;1)(A|B)_GHIJ_(A-F)'

def expandDash(trend):
dashCount = trend.count("-")
for dC in range(0, dashCount):
dashIndex = trend.index("-")-1
trendRange = trend[dashIndex:]
bareTrend = trend[0:trend.index("(")]
beginRange = trendRange[0:trendRange.index("-")]
endRange = trendRange[trendRange.index("-"):trendRange.index(";")]
trendIncrement = trendRange[-1]
expandedTrendList = []


def regexExpand(trend):

for regexTrend in trend.split(')'):
if "-" in regexTrend:
print trend
expandDash(regexTrend)

我显然被困在这里......

有什么简单的方法可以使用 REGEX 进行字符串扩展吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com