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
  • 40,483
  • 12
  • 109
  • 137
  • 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

2 Answers2

2

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
  • 3,731
  • 21
  • 37
0

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
  • 12,723
  • 7
  • 48
  • 99