简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM