python re module

15 views
Skip to first unread message

Degenerate Tech

unread,
Nov 4, 2020, 12:48:10 PM11/4/20
to Kivy users support
i have a html text
video link starting after sd_src: key 

sd_src:"http//video.mp4"

i want to find video link from html text
how to do using re module in python?
 



Elliot Garbus

unread,
Nov 4, 2020, 4:38:33 PM11/4/20
to kivy-...@googlegroups.com

Here are some useful resources:

https://docs.python.org/3/howto/regex.html

 

An interactive tool for creating (and learning regex): https://regex101.com/

 

 

>>> import re

>>> p = re.compile(r'(sd_src:\s?)(\".+\")')

>>> m = p.match('sd_src:"http//video.mp4"')

>>> matches = m.groups()

>>> matches[1]

'"http//video.mp4"'

 

Explanation:  from the regex101 tool.

"

(sd_src:\s?)(\".+\")

"

gm

1st Capturing Group 

(sd_src:\s?)

sd_src: matches the characters sd_src: literally (case sensitive)

\s?

 matches any whitespace character (equal to [\r\n\t\f\v ])

? Quantifier — Matches between zero and one times, as many times as possible, giving back as needed (greedy)

2nd Capturing Group 

(\".+\")

\" matches the character " literally (case sensitive)

.+

 matches any character (except for line terminators)

+ Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)

\" matches the character " literally (case sensitive)

Global pattern flags

g modifier: global. All matches (don't return after first match)

m modifier: multi line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/aaae851e-db8f-4348-a183-e19073cfc981o%40googlegroups.com.

 

Degenerate Tech

unread,
Nov 5, 2020, 1:56:14 AM11/5/20
to Kivy users support
please see this ..it is not matching
import re 
p='hello this is line  ,sd_src:"https://video?=j.mp4"'
m=re.compile(r'(sd_src:\s?)(\".+\")')



v=m.match(p)
url=v.groups()
#url = re.search('sd_src:"https', )
#print(x)
print(url)

######
 Traceback (most recent call last):
  File "main.py", line 35, in <module>
    url=v.groups()
AttributeError: 'NoneType' object has no attribute 'groups'

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-...@googlegroups.com.

Elliot Garbus

unread,
Nov 5, 2020, 8:37:35 AM11/5/20
to kivy-...@googlegroups.com

See:

https://docs.python.org/3/howto/regex.html#performing-matches

 

use search instead of match,  match is for checking the re at the beginning of a string.

 

>>> string = 'hello this is line  ,sd_src:"https://video?=j.mp4"'

>>> m = re.compile(r'(sd_src:\s?)(\".+\")')

>>> result = m.search(string)

>>> url = result.groups()[1]

>>> url

'"https://video?=j.mp4"'

To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ff30c272-b761-4c16-b5b3-12e216e00a06o%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages