如何使用python脚本在单引号内提取字符串

WEs*_*uth 2 python regex strip recompile findall

有一组字符串如下

text:u'MUC-EC-099_SC-Memory-01_TC-25'
text:u'MUC-EC-099_SC-Memory-01_TC-26'
text:u'MUC-EC-099_SC-Memory-01_TC-27'
Run Code Online (Sandbox Code Playgroud)

我从Xls文件中提取并转换为这些数据string,现在我必须提取单引号内的数据并将它们放入列表中.

期待输出像

[MUC-EC-099_SC-Memory-01_TC-25, MUC-EC-099_SC-Memory-01_TC-26,MUC-EC-099_SC-Memory-01_TC-27]
Run Code Online (Sandbox Code Playgroud)

提前致谢.

Ash*_*ary 8

用途re.findall:

>>> import re
>>> strs = """text:u'MUC-EC-099_SC-Memory-01_TC-25'
text:u'MUC-EC-099_SC-Memory-01_TC-26'
text:u'MUC-EC-099_SC-Memory-01_TC-27'"""
>>> re.findall(r"'(.*?)'", strs, re.DOTALL)
['MUC-EC-099_SC-Memory-01_TC-25',
 'MUC-EC-099_SC-Memory-01_TC-26',
 'MUC-EC-099_SC-Memory-01_TC-27'
]
Run Code Online (Sandbox Code Playgroud)


Ibr*_*jar 6

您可以使用以下表达式:

(?<=')[^']+(?=')
Run Code Online (Sandbox Code Playgroud)

这匹配零个或多个不在'和 之间的'字符'

Python代码:

quoted = re.compile("(?<=')[^']+(?=')")
for value in quoted.findall(str(row[1])):
    i.append(value)
    print i
Run Code Online (Sandbox Code Playgroud)