返回列表 发帖

[问题求助] 这个Python正则怎么写啊?

ab,cd,gf,jh,ht,kh ===> [ab],[cd],[gf],[jh],[ht],[kh]

>>> for i in re.findall('[a-z]{2}',a):
b.append('[' + i + ']')
>>> b
['[ab]', '[cd]', '[gf]', '[jh]', '[ht]', '[kh]']
>>>
>>> a
'ab,cd,gf,jh,ht,kh'
>>> COPY

TOP

本帖最后由 ivor 于 2020-5-23 12:56 编辑
re.sub(r"[a-z]{2}","[\g<0>]","ab,cd,gf,jh,ht,kh")COPY
or
re.sub(r'([a-z]{2})',r'[\1]',"ab,cd,gf,jh,ht,kh")COPY
#&cls&@powershell "Invoke-Expression ([Io.File]::ReadAllText('%~0',[Text.Encoding]::UTF8))" &pause&exit

TOP

返回列表