-1

Objective

  • I'm looking to use the regular expression \d+ to extract just the digits from the string, answer_40194.

Problem

  • I'm targeting a form element with Selenium and I'm printing the formID to the Terminal, but after the line re.findall('\d+', formID) I expect formID to be just the numbers 40194, but instead I'm getting the entire string answer_40194.

script.py

import selenium
import re

form = browser.find_element_by_tag_name('form')
formID = form.get_attribute('id')
re.findall('\d+', formID)
print formIDNumber
Community
  • 1
  • 1
Andrew Nguyen
  • 1,416
  • 4
  • 21
  • 43

1 Answers1

2

You need to assign the result to a variable, e.g.

var1 = re.findall('\d+', formID)
print(var1)

This will generate a list, if you only want one result, use

var1 = re.search('\d+', formID)
print(var1.group(0))

The latter is called a regular expression object, hence the .group(0), see the documentation on python.org for more information.

Jan
  • 42,290
  • 8
  • 54
  • 79