1
C:\Users\dibyajyo>python                                                              
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32    
Type "help", "copyright", "credits" or "license" for more information.                
>>>                                                                                   
>>>                                                                                   
>>> import re                                                                         
>>> outString = 'DiagnosticPath = fs0:\\efi\\tools'                                   
>>> print outString                                                                   
DiagnosticPath = fs0:\efi\tools                                                       
>>> expString = 'DiagnosticPath = fs0:\\efi\\tools'                                   
>>> print expString                                                                   
DiagnosticPath = fs0:\efi\tools                                                       
>>> matchObj = re.search( expString, outString, re.M|re.I)                            
>>> if matchObj:                                                                      
...   print matchObj.group()                                                          
...                                                                                   
>>>

Both expString and outString strings are the same yet not sure why I am not finding any match...

vaultah
  • 44,105
  • 12
  • 114
  • 143
  • 3
    Have you tried using a raw String for the expression? `expString = r'DiagnosticPath = fs0:\\efi\\tools'` – Felk May 11 '15 at 16:19
  • This is exactly what I wanted to post after trying it. Was late for 31 seconds. – lanenok May 11 '15 at 16:20
  • Using a raw string is a good idea in general for regexes, but **not relevant to the actual problem**. The problem here is that we want to search for a literal sequence of text, using a regex; this requires escaping through `re.escape`, as in the linked duplicate. – Karl Knechtel Aug 08 '22 at 02:48

2 Answers2

1

Quote from python's re documentation:

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal.

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'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

So if you use:

expString = r'DiagnosticPath = fs0:\\efi\\tools'

your code should work.

sirfz
  • 4,097
  • 23
  • 37
  • While it **happens** to work for **this specific input**, using a raw string like this is *conceptually wrong* for the problem. It solves the wrong problem: we aren't concerned with writing the string neatly in the source code, but with *making a regular expression* that correctly matches some literal text, given that literal text. The **correct** tool for this job is `re.escape`. I am not writing an answer because this question is a duplicate. – Karl Knechtel Aug 07 '22 at 09:20
-1

See this answer

import re

expString = r'DiagnosticPath = fs0:\\efi\\tools'
outString = 'DiagnosticPath = fs0:\\efi\\tools'

matchObj = re.search(expString, outString, re.M|re.I)

print matchObj.group()
Community
  • 1
  • 1
John
  • 13,197
  • 7
  • 51
  • 101
  • While it **happens** to work for **this specific input**, using a raw string like this is *conceptually wrong* for the problem. It solves the wrong problem: we aren't concerned with writing the string neatly in the source code, but with *making a regular expression* that correctly matches some literal text, given that literal text. The **correct** tool for this job is `re.escape`. I am not writing an answer because this question is a duplicate. – Karl Knechtel Aug 07 '22 at 09:20