有人可以告诉我如何在 Python 正则表达式搜索中使用变量 url 吗?Url 是传递给保存下面代码的函数的变量。我尝试了以下方法,但不断出现错误。希望有人能帮助我解决此错误。提前致谢。 示例 str 和 url 值:
str='.......href="http://somewebsite:8080/hls/1.m3u8?session=34534525".....'
url='http://somewebsite:8080/hls/1.m3u8?session='
Python 代码:
foundValue= re.search('+url+(.*)"', str)
print 'foundValue:'
print foundValue.group(1);
xbmc 日志上的错误:
ERROR: EXCEPTION Thrown (PythonToCppException) :
-->Python callback/script returned the following error<--
- NOTE: IGNORING THIS CAN LEAD TO MEMORY LEAKS!
Error Type: <class 'sre_constants.error'>
Error Contents: nothing to repeat
Traceback (most recent call last):
File "F:\......\addon.py", line 281, in <module>
play_video(url)
File "F:\.......\addon.py", line 160, in play_video
foundValue = re.search('+url+(.*)"', str)
File "F:\.....\XBMC\system\python\Lib\re.py", line 142, in search
return _compile(pattern, flags).search(string)
File "F:\....\XBMC\system\python\Lib\re.py", line 242, in _compile
raise error, v # invalid expression
error: nothing to repeat
-->End of Python script error report<--
最佳答案
我猜你想要这个:
foundValue= re.search(re.escape(url)+r'(.*?)"', str1)
也不要使用 str
,因为它是内置的。
关于python - 如何在Python正则表达式搜索中使用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34073749/