alxc
alxc

Reputation: 11

Python regex to match a quoted string

Really sorry about posting a regex question. Here it is :

I'm trying to match an URL containing a quote, contained in a LIST, like so :

urls = ["my.url.com/Som'Link"]

I match it with another list, that only contains the last word of the URL, like so :

match_list = ["Som'Link"]

I test like this :

p = match_list[0] + "$" #(I need the $ to make sure the word is the last of the URL)
s = urls[0]
if re.search(p, s):
    #Use that link in some way.

I am completely unable to find a way to make it match. I tried :

p = r"" + match_list[0] + "$"

no luck.

p = compile(r"" + match_list[0] + "$")

no more luck.

Note : I tried those two lines with re.escape arround my match_list[0] too... Doesn't work !!

I hope my submission is OK. Just comment and I will improve it if necessary.

Upvotes: 0

Views: 142

Answers (1)

Pythonista
Pythonista

Reputation: 11645

You don't need a regex for this you can just test:

if "Som'Link" in urls[0]

This will test whether "Som'Link" occurs anywhere in the url.

Assuming your urls always end with the string you're trying to match the end only you can use endswith

Upvotes: 1

Related Questions