0

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.

3
  • 2
    don't use regex. also: look at the difference between re.match() and re.search()! Commented Apr 16, 2016 at 16:44
  • 1) The others are right that your problem doesn't need regex. (2) Your example code worked fine for me: pastebin.com/vLqq1nSQ Commented Apr 16, 2016 at 16:51
  • sorry 'bout the match/search thing… i somehow thought you had used match Commented Apr 16, 2016 at 16:58

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

If the urls stay of this form, but I used in if you want to test for matches for additions to the url.
Specifically urls[0].endswith("Som'Link")
Thanks! My error was also somewhere else btw... Useful method anyway :)
Can I "close" my topic ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.