[英]Regular expression stop capture group at ? symbol
I am trying to capture "thing1"
and "thing2"
in these strings: 我试图在这些字符串中捕获
"thing1"
和"thing2"
:
http://www.example.com/stuff1/thing1
http://www.example.com/stuff2/thing2?id=9999
In python, I tried this: 在python中,我尝试了以下操作:
re.findall('^.*(?:stuff1/|stuff2/)(.*)\??.*$', url)
The first example URL works with my code, but the second gives: 第一个示例URL适用于我的代码,但是第二个示例给出:
['thing2?id=9999']
I was intending for the capture group to not include the "?"
我打算让捕获组不包含
"?"
. 。
Try this: 尝试这个:
import re
re.findall('^.*(?:stuff\d\/)(.*?)(?=\?|$).*$', url)
In this regex, I used Positive Lookahead (?= )
to exclude ?
在此正则表达式中,我使用正向前瞻
(?= )
排除了?
只需捕获“非?
”字符:
re.findall('^.*(?:stuff1/|stuff2/)([^?]*).*$', url)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.