我需要一个正则表达式,它与下面的规则匹配api路径
因此,在本例中,只有前两个元素必须生成匹配。
发布于 2017-05-29 10:03:32
我觉得这应该能帮到你。但是,正如在评论中所说的,我认为考虑到您的描述,/allGear{Exe
也应该包括在内。我给出的代码也会返回以下内容
x = ["/word/worD/sdfsfsd","/fsdfsdf","/","/{sfsdf",'/','/_','/{','/{"type":"tnt"}',"/allGear{Exe","/Grear"]
import re
for i in x:
pattern = re.search("""\/[a-z][a-zA-Z]+""", i, re.S)
#If you don't want the /allGear, change the regex to """\/[a-z][a-zA-Z]+$""";
if(pattern is not None):
print i
发布于 2017-05-29 10:04:20
使用以下方法:
import re
x = ["/word/worD/sdfsfsd","/fsdfsdf","/","/{sfsdf",'/','/_','/{','/{"type":"tnt"}',"/allGear{Exe","/Grear"]
result = [p for p in x if re.match(r'^(\/[a-z][a-zA-z]+)+$', p)]
print(result)
产出:
['/word/worD/sdfsfsd', '/fsdfsdf']
https://stackoverflow.com/questions/44239573
复制相似问题