Search code examples
pythonregexpreg-match

How explode and regex in Python


I have this Link :

http://www.mySite.come/part1/one-two-12/one-two-three-four-x36-250g-P469198/

i want to chek if the last part starts with 'P' and numbers like : P432432

how can i explode and check my condition?

i tried:

url="http://www.mySite.come/part1/one-two-12/one-two-three-four-x36-250g-P469198/"
url.s.split('-')

Solution

  • You can use

    re.sub("[^\w]", "" , url.split('-')[-1])
    

    to get the last part and validate it using

    re.match("P\d+", re.sub("[^\w]", "", url.split('-')[-1]))
    

    or

    re.search("P\d+", re.sub("[^\w]", "", url.split('-')[-1]))