python - 如何在Python正则表达式搜索中使用变量?

标签 python xbmc kodi

有人可以告诉我如何在 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/

相关文章:

Linux - 使用 SSH 的关闭脚本

python - 如何识别变量是类还是对象

python - Neo4j 驱动程序 - 确认交易成功

python - Pandas 绘制一个重复的数据框

python - bat 文件在使用 Python 的另一个文件夹中时不运行

python - 如何在python中更改图像

python - 如何在xbmc 的标签控件中输入字符串?

python - 使用 python/beautiful soup 从网站上抓取链接作为 Kodi 插件

batch-file - 批量命令删除具有特定名称的所有子文件夹