I am trying to identify the 10 digit number in a directory name, which may look like this: d:\research\python\test\2020\0001766016-20-000002.txt
My code is as follows:
import re
text="d:\research\python\test\2020\0001766016-20-000002.txt"
x=re.search(r"(?<=\\)\d{10}(?=-)",text)
print(x)
I got None as a result. Any suggestions?
>Solution :
The issue is with your string that contains \
You need to encapsulate the string like this r'd:\research\python\test\2020\0001766016-20-000002.txt'
import re as regex
text = r'd:\research\python\test\2020\0001766016-20-000002.txt'
x = regex.search(r"(?<=\\)\d{10}(?=-)", text)
print(x.group(0))
0001766016
reference: https://www.pythontutorial.net/python-basics/python-raw-strings/