Home > Enterprise >  find numbers in string in selenium
find numbers in string in selenium

Time:11-27

In Python and Selenium, how do I find numeric characters in a text and put them in a variable? for example :

text = Your verification code is: 5674

I need to find the number 5674 from the text and put it in a variable.

Result »» x = 5674


import re txt = "Your verification code is: 5674" x = is_digit(txt) print(x)

x »»» 5674

CodePudding user response:

If the message is always formatted like this you can do:

text[text.find(":"):].strip() # -> 5764

or

text.strip("Your verification code is:") # -> 5764

CodePudding user response:

Well if you really expect just a single verification integer at the end, you could use re.search():

text = "Your verification code is: 5674";
code = re.search(r'\d $', text).group()
print(code)  # 5674
  • Related