1

could any one show me how i can use variable url inside Python a regular expression search?Url is the variable passed to the function that holds the code below. I tried the following but keep getting error.Hope some help me fix this error.Thanks in advance. sample str and url value:

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

python code:

foundValue= re.search('+url+(.*)"', str)
print 'foundValue:'
print foundValue.group(1);

error on xbmc logs:

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<--
user1788736
  • 2,727
  • 20
  • 66
  • 110

2 Answers2

2

Guess you wanted this:

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

Also don't use str as it is in-built.

halfer
  • 19,824
  • 17
  • 99
  • 186
vks
  • 67,027
  • 10
  • 91
  • 124
0

Another option is to use format to make the string:

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

Note that the standard for variable names in Python is words_separated_by_underscores (see PEP 008), and as @vks mentioned, avoid use of str as a variable as it shadows the builtin str class.

MPlanchard
  • 1,798
  • 1
  • 16
  • 13