user3496483
user3496483

Reputation: 35

Python Regular Expression Define Start End

How would I use regular expression to find only links that end with numbers

i've tried:

links = "'http://www.badlink.com' , 'http://good.link.com/W0QQAdIdZ567296978'"  
re.findall(r'http://[\w\.\w\.\w\.-]+.*',links)   

I don't know how to make python stop searching after it finds integers in the link. Best case scenario I would like the match to only occur if the link ends with (5) or more numbers

Upvotes: 0

Views: 75

Answers (2)

perreal
perreal

Reputation: 97938

If a single number at the end is good enough:

 good_links = filter(lambda x: x.startswith("http://") and 
               all(c in "0123456789" for c in x[-4:]), links.split("'"))

Upvotes: 1

elixenide
elixenide

Reputation: 44823

Try this:

re.findall(r'^http://.+?\d{5,}$',links)

Visual explanation:

Regular expression visualization

Debuggex Demo

Upvotes: 1

Related Questions