0

I have a fairly simple regular expression pattern that I'm trying to match with some raw string literals. I've tested the pattern here (https://regex101.com/r/xT6xU8/2) and it works perfectly, but in my code (snippet below), it doesn't work.

import sys
import re
item = "Aarskog\xc3\xa2\xc2\x80\xc2\x93Scott syndrome"
item = item.encode("string-escape") 
pattern = re.compile("\\[a-zA-Z0-9]{3}")
if pattern.search(item) != None:
  print "YES"
sys.exit()

I can't understand why the pattern isn't matching the string. I've tried matching replacing item in pattern.search() with the regular string, the raw string using r, etc. Nothing works.

Thanks for the help!

Try431
  • 217
  • 6
  • 16
  • How does the pattern being a raw string not remove the power of the regex shortcuts (e.g., {3}, [a-z], etc.)? – Try431 Jul 24 '15 at 16:24
  • Also you don't need to explicitly call `sys.exit`. python scripts will automatically exit. – NightShadeQueen Jul 24 '15 at 16:26
  • sys.exit() was to terminate the program so the rest of my code wouldn't run. This was just a snippet, though I probably shouldn't have included the sys.exit() in the snippet. Thanks for the assistance though! – Try431 Jul 24 '15 at 17:42

0 Answers0