-2

I am trying to pass variables using a regular expression (re.search) but I can't seem to successfully read the information into match.group(1), (2) etc. using the code below. Could someone take a quick look to see where I'm going wrong?

serial = 'abcdeID:11111abcdePR:22222abcde'

id = 11111
pr = 22222

match =  re.search(r'ID:{0}PR:{1}'.format(id, pr), serial)
print("ID value returned = " + match.group(1))
print("PR value returned = " + match.group(2))

#output
#AttributeError: 'NoneType' object has no attribute 'group'
mrzasa
  • 22,895
  • 11
  • 56
  • 94
SamL
  • 133
  • 2
  • 3
  • 16
  • What exactly are you trying to do here? Are you just trying to see if the string `ID:11111PR:22222` is a substring of serial? Or do you want to extract the two numbers from the serial (i.e. you don't know what `id` and `pr` are, and want to extract them from `serial`)? – Mathias-S Sep 12 '19 at 08:30

1 Answers1

0

You have no groups (...) in your regex, so they're not returned. Add them like that:

match =  re.search(r'ID:({0})PR:({1})'.format(id, pr), serial)
mrzasa
  • 22,895
  • 11
  • 56
  • 94
  • Hi mrzasa, thanks for your reply. However I'm still getting the same error: AttributeError: 'NoneType' object has no attribute 'group' after making the changes you suggested. Seems strange – SamL Sep 12 '19 at 10:16