-2

I have a regex file that I am reading in and compiling. The issue I am having is the regex will sometimes leading \.

Z\\d*\\.file_\\.?

instead of

Z\d*\.file_\.?

It sometimes matching but others not.

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
user3525290
  • 1,557
  • 2
  • 20
  • 47

2 Answers2

1

Most likely it does not match when you use raw string and double backslash.

s = "ABC 23"

re.findall('\d+',s)
['23']

re.findall(r'\d+',s)

['23']

re.findall('\\d+',s)
['23']

re.findall(r'\\d+',s)     

[]
LetzerWille
  • 5,355
  • 4
  • 23
  • 26
0

I don't know if that is what you want but if you read documentation of regular expression operations

It says :

"Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning"

And also :

"The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'"

Example:

regex= re.compile(r'string')
tolis
  • 1
  • 1