首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >匹配API路径的Python regex

匹配API路径的Python regex
EN

Stack Overflow用户
提问于 2017-05-29 09:55:47
回答 2查看 339关注 0票数 0

我需要一个正则表达式,它与下面的规则匹配api路径

  1. 路径以'/‘开头,但可以有多个'/’
  2. '/‘必须后面跟着一个以小写开头的单词,但后面可以有大写 X= "/word/worD/sdfsfsd“、"/fsdfsdf”、"/“、"/{sfsdf”、'/‘、'/_’、'/{‘、’/{‘/{“类型”:“tnt”}’/allGear{Exe‘、"/Grear“导入re pattern =re.compile(”(/a+)+“)

因此,在本例中,只有前两个元素必须生成匹配。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-29 10:03:32

我觉得这应该能帮到你。但是,正如在评论中所说的,我认为考虑到您的描述,/allGear{Exe也应该包括在内。我给出的代码也会返回以下内容

代码语言:javascript
运行
复制
 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
票数 1
EN

Stack Overflow用户

发布于 2017-05-29 10:04:20

使用以下方法:

代码语言:javascript
运行
复制
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)

产出:

代码语言:javascript
运行
复制
['/word/worD/sdfsfsd', '/fsdfsdf']
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44239573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档