-2

I want to enumerate '4.9.8' from this line. how can i get that. i am new in Regex.

import re
url  = 'http://variety.com'
line = "<script type=\'text/javascript\' src=\'https://js-sec.indexww.com/htv/htv-jwplayer.min.js?ver=4.9.8\'></script>"
regex = "?ver=(.+?)\'>"
version = re.findall(regex,line)
print(version)
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50
Shivam Raj
  • 137
  • 1
  • 11
  • 1
    `?` is a special char in regex that makes the pattern it modifies optional (matches 1 or 0 times). [Escape it, `\?`.](https://ideone.com/xD1UO4) – Wiktor Stribiżew Oct 22 '18 at 10:23
  • @Shivam Raj Please don't update the code in the question according to the answers you got, otherwise the question doesn't make sense anymore - I wondered for a while what the problem was, as I was looking at the corrected code. I rolled back your edit to the original version. – Thierry Lathuille Oct 22 '18 at 22:46

1 Answers1

0

Escape ? special character :

import re
url  = 'http://variety.com'
line = "<script type=\'text/javascript\' src=\'https://js-sec.indexww.com/htv/htv-jwplayer.min.js?ver=4.9.8\'></script>"
regex = "\?ver=([0-9.]+)"
version = re.findall(regex,line)
print(version)

Returns :

['4.9.8']

I used "\?ver=([0-9.]+)" regexp but you can also use yours like this : regex = "\?ver=(.+?)\\'>" (escape \ too)

Corentin Limier
  • 4,946
  • 1
  • 13
  • 24