问题描述
我需要从具有以下格式的字符串中匹配字符串"foo":
I need to match the string "foo" from a string with this format:
string = "/foo/boo/poo"
我绑定了以下代码:
poo = "poo"
foo = re.match('.*(?=/' + re.escape(poo) + ')', string).group(0)
,它给了我/foo/boo
作为变量foo的内容(而不只是foo/boo
).
and it gives me /foo/boo
as the content of the variable foo (instead of just foo/boo
).
我尝试了以下代码:
poo = "poo"
foo = re.match('(?=/).*(?=/' + re.escape(poo) + ')', string).group(0)
,我得到相同的输出(/foo/boo
而不是foo/boo
).
and I'm getting the same output (/foo/boo
instead of foo/boo
).
我怎么只匹配foo/boo
部分?
推荐答案
嘿,尝试以下正则表达式:
Hey try the following regex:
(?<=/).*(?=/poo)
^^^^^^
它不会考虑您在结果中的第一个斜杠.
It will not take into account your first slash in the result.
经过测试的 regex101 : https://regex101.com/r/yzMkTg/1
通过以下方式转换代码,它应该可以工作:
Transform your code in the following way and it should work:
poo = "poo"
foo = re.match('(?<=/).*(?=/' + re.escape(poo) + ')', string).group(0)
快速浏览此链接,以获取有关Positive lookahead
和Positive lookbehind
Have a quick look at this link for more information about the behavior of Positive lookahead
and Positive lookbehind
http://www.rexegg.com/regex-quickstart.html
这篇关于Python正则表达式的回顾与展望的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!