1

Python 内で正規表現検索で変数 url を使用する方法を教えてもらえますか?Url は、以下のコードを保持する関数に渡される変数です。次のことを試しましたが、エラーが発生し続けます。このエラーを修正するのに役立つことを願っています。よろしくお願いします。サンプルの str と url の値:

str='.......href="http://somewebsite:8080/hls/1.m3u8?session=34534525".....'
url='http://somewebsite:8080/hls/1.m3u8?session='

パイソンコード:

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<--
4

2 に答える 2

2

あなたはこれが欲しかったと思います:

foundValue= re.search(re.escape(url)+r'(.*?)"', str1)

またstr、内蔵していますので使用しないでください。

于 2015-12-03T18:45:41.650 に答える