import re
value = "world_wide='test1/one/two', " \
"stage_test='ALPHA', world_wide='test2/one/two', " \
"stage_test='GAMMA', world_wide='test3/one/two', " \
"stage_test='GAMMA', world_wide='test4/one/two', " \
"stage_test='ALPHA', world_wide='test5/one/two', " \
"stage_test='GAMMA', world_wide='test6/one/two', " \
"stage_test='GAMMA"
pattern = r"(world_wide=\'.*\')"
for match in re.findall(pattern, str(value)):
print ("\n", match)
尝试过滤给定字符串值的特定输出。上面的代码给出了以下输出:
world_wide='test1/one/two', stage_test='ALPHA', world_wide='test2/one/two', stage_test='GAMMA', world_wide='test3/one/two', stage_test='GAMMA', world_wide='test4/one/two', stage_test='ALPHA', world_wide='test5/one/two', stage_test='GAMMA', world_wide='test6/one/two', stage_test='
我想要得到的是:如果字符串匹配特定条件,例如:
如果找到 'world_wide=',则返回两个字符之间的以下值。在这种情况下,这将是两个单引号,不包括 '/one/two'。
期望的输出:
>>>test1
test2
test3
test4
.........
请您参考如下方法:
您可以使用以下表达式:
world_wide='([^/]+)
# world_wide='
# capture anything not a / into group 1
在
Python 中是:
import re
value = "world_wide='test1/one/two', " \
"stage_test='ALPHA', world_wide='test2/one/two', " \
"stage_test='GAMMA', world_wide='test3/one/two', " \
"stage_test='GAMMA', world_wide='test4/one/two', " \
"stage_test='ALPHA', world_wide='test5/one/two', " \
"stage_test='GAMMA', world_wide='test6/one/two', " \
"stage_test='GAMMA"
rx = re.compile(r'''world_wide='([^/]+)''')
parts = rx.findall(value)
print(parts)
这会产生一个包含
的列表['test1', 'test2', 'test3', 'test4', 'test5', 'test6']
