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\u wide=”,请在两个字符之间返回以下值。在本例中,这将是两个单引号,不包括'/one/two'。你知道吗
期望输出:
>>>test1
test2
test3
test4
.........
一旦我要处理regex,我总是使用https://regex101.com,这对于快速原型和多案例测试来说非常好和容易。有关正则表达式,请参见以下链接https://regex101.com/r/TLXDXl/1。我建议的模式是:
r'世界范围='(?Ptest\d+/'
哪个筛选器尝试使用“world\u wide=”匹配前缀,然后匹配名为“name”的组,该组以“test”开头,后跟一个或多个数字。更一般的方法是使用“[\w\d]+”来表示匹配任何字母和数字的名称。你知道吗
希望有帮助
您要查找的正则表达式可能与
pattern = r"world_wide='(.*)\/one"
一样简单。这里有一个演示:https://regexr.com/3qffn让你的问题特别难以回答的是,我认为你的例子中有一些拼写错误。在第6行你有
stage_test='GAMMA')>]",
我想你实际上是指stage_test='GAMMA')>,
。是这样吗?你知道吗可以使用以下表达式:
在
Python
中,这是:这将生成一个包含
见a demo on regex101.com。你知道吗
相关问题 更多 >
编程相关推荐