Breaking

Python Regexp With Re Module | Extract Phone Number, Youtube Video ID, Paylist ID From URL

 A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).

Know more about regular expression



Get digits with regular expression

import re

meet = "Meet me at 07:30 AM in Dhaka, call on +880-1721-514285 "\
"or +8801824214285 01641514285 01641-514285"
pattern = "\d"
regexp = re.findall(pattern, meet)
# print(regexp)





pattern = "\d+"
regexp = re.findall(pattern, meet)
# print(regexp)


Get time with regular expression

import re

meet = "Meet me at 07:30 AM in Dhaka, call on +880-1721-514285 or +8801824214285"\
" 01641514285 01641-514285"


chunk_1 ="\d{2}"
chunk_2 = ":"
chunk_3 = "\d{2}"
chunk_4 = "\sAM|PM"

pattern = f"{chunk_1}{chunk_2}{chunk_3}{chunk_4}"
regexp = re.findall(pattern, meet)
print(regexp)


Get numbers with regular expression

import re

meet = "Meet me at 07:30 AM in Dhaka, call on +880-1721-514285 "\
"or +8801824214285 01641514285 01641-514285"


chunk_1 ="\+?\d{1,}-?"
chunk_2 = "\d{4}-?"
chunk_3 = "\d{6}"


pattern = f"{chunk_1}{chunk_2}{chunk_3}"
regexp = re.compile(pattern)
print(regexp.findall(meet))


Get a youtube base URL and learn about groups

import re

yt_playlist = "https://youtube.com/watch?v=tsjIfJVYqmQ&list"\
"=PLDbgPtNOy-yDNofX93dyYVsPWs5gc0Lis http://youtube.com/watch?"\
"v=tsjIfJVYqmQ&list=PLDbgPtNOy-yDNofX93dyYVsPWs5gc0Lis"\
" http://www.youtube.com/watch?v=tsjIfJVYqmQ&list=PLDbgPtNOy-"\
"yDNofX93dyYVsPWs5gc0Lis"

chunk_01 = "(?P<protocol>https\:\/\/|http\:\/\/)?"
chunk_02 = "(?P<web>www\.)?"
chunk_03 = "(?P<domain>youtube\.com)"

pattern= f"{chunk_01}{chunk_02}{chunk_03}"

reg_exp = re.compile(pattern)

# group_list_name = reg_exp.match(yt_playlist).groupdict()
# print(group_list_name["domain"])

print(reg_exp.findall(yt_playlist))


Get youtube video ID with regular expression

import re

yt_playlist = "https://youtube.com/watch?v=tsjIfJVYqmQ&list="\
              "PLDbgPtNOy-yDNofX93dyYVsPWs5gc0Lis"

chunk_01 = "(?<=v\=)"
chunk_02 = "\w+"
chunk_03 = "(?=\&)"

pattern= f"{chunk_01}{chunk_02}{chunk_03}"

reg_exp = re.compile(pattern)


print(reg_exp.findall(yt_playlist))


Obtain youtube playlist ID with regular expression

import re

yt_playlist = "https://youtube.com/watch?v=tsjIfJVYqmQ"\
"&list=PLDbgPtNOy-yDNofX93dyYVsPWs5gc0Lis"

chunk_01 = "(?<=list\=)"
chunk_02 = "\w+"

pattern= f"{chunk_01}{chunk_02}"

reg_exp = re.compile(pattern)


print(reg_exp.findall(yt_playlist))




No comments:

Powered by Blogger.