Python regex lookbehind and lookahead

穿精又带淫゛_ 提交于 2020-05-26 02:44:08

问题


I need to match the string "foo" from a string with this format:

string = "/foo/boo/poo"

I tied this code:

poo = "poo"
foo = re.match('.*(?=/' + re.escape(poo) + ')', string).group(0)

and it gives me /foo/boo as the content of the variable foo (instead of just foo/boo).

I tried this code:

poo = "poo"
foo = re.match('(?=/).*(?=/' + re.escape(poo) + ')', string).group(0)

and I'm getting the same output (/foo/boo instead of foo/boo).

How can I match only the foo/boo part?


回答1:


Hey try the following regex:

(?<=/).*(?=/poo)
^^^^^^

It will not take into account your first slash in the result.

Tested 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)

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




回答2:


You are missing a < in your lookbehind!

Lookbehinds look like this:

(?<=...)

not like this:

(?=...)

That would be a lookahead!

So,

(?<=/).*(?=/poo)


来源:https://stackoverflow.com/questions/47886809/python-regex-lookbehind-and-lookahead

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!