Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试在 python 中创建一个正则表达式,它执行以下操作:
在“/home/python/app/index.html”行中,它搜索“app”之前的任何内容并删除该文本,即在这种情况下返回“app/index.html”。
您可能想在您的正则表达式中使用“组”,如下所示:
>>> s = re.search(r".*\/(app.*)", "/home/python/app/index.html") >>> s.groups()[0] 'app/index.html'
请注意,在这个正则表达式中,只有一个组——“app”后面跟着字符串的其余部分——但不仅仅是任何“app”,只有一个前面有一个斜杠。请注意,斜线不包含在圆括号中 - 因此不在组中(根据您的问题看起来这就是您想要的)。