1

How to get this string "534641" (this value is dynamic, can be 6,5,4 digits)? How to find "-" before "534641"?

import re

string = "http://www.test.com.my/white-red-gift-perfume-powerbank-yellow-534641.html?ff=1\u0026s=Ebsr"
m = re.search('-(.+?).html', string).group(1)
print (m)

https://repl.it/JSxp

2 Answers 2

2

You are almost there. Since what you want is only digits, you could use \d to capture only digits:

>>> m = re.search('-(\d+).html', string).group(1)
>>> print (m)
534641

Another way would be to tell 'all characters excepts -':

>>> m = re.search('-([^-]+).html', string).group(1)
>>> print (m)
534641

For more info, see the doc.

Some quick notes: the .html should be \.html, avoid using names such as 'string', 'list' that are used by python. It could go wrong without knowing why.

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

1 Comment

awesome answer, will mark this as answer, thanks for the help +1
1

You already have the number at the end. Just split on the dashes using:

m = re.search('-(.+?).html', string).group(1).split("-")
# last element in m is the number you are looking for
print (m[-1])

Comments

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.