Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Python正则表达式搜索中使用变量?_Python_Xbmc_Kodi - Fatal编程技术网

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

如何在Python正则表达式搜索中使用变量?,python,xbmc,kodi,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=

有谁能告诉我如何在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<--
错误:引发异常(PythonToCppException):
-->Python回调/脚本返回了以下Python脚本错误报告的errorEnd

我猜您想要这个。也不要使用
str
,因为它是内置的。

另一个选项是使用格式生成字符串:

found_value = re.search(r'{0}(.*)"'.format(url), search_string)

请注意,Python中变量名的标准是用下划线分隔的单词(请参阅),正如@vks所提到的,避免使用
str
作为变量,因为它会隐藏内置的
str
类。

感谢您的回复。现在我得到了这个错误:错误类型:错误内容:“NoneType”对象没有属性“group”回溯(上次的最新调用):print foundValue.group(1);AttributeError:“非类型”对象没有属性“组”-->Python脚本结束错误报告
found_value = re.search(r'{0}(.*)"'.format(url), search_string)