-2

I am trying to find a regular expression to return to me the entire 6 digits with the first 3 digits as a pattern/fixed.

Ex:

import re
string_ex = 'docs/data/622999/2013904065003_file.bin'

re.findall(r'622(\d{3})',string_ex)

results in just ['999'] but I want the result to be ['622999']

Thanks!

Wondercricket
  • 7,651
  • 2
  • 39
  • 58

2 Answers2

0

You should include 622 too within the parenthesis

>>> import re
>>> string_ex = 'docs/data/622999/2013904065003_file.bin'
>>> re.findall(r'(622\d{3})',string_ex)
['622999']
Prem Anand
  • 2,469
  • 16
  • 16
-1

You can use "index" on the string directly.

i = string_ex.index("622")
found = string_ex[i-3:i+2]

https://www.tutorialspoint.com/python/string_index.htm