Commits

Victor Stinner committed 6e64584

Fix parser: support backslash in a range, eg. parse(r"[a\]x]")

Comments (0)

Files changed (2)

hachoir-regex/README

 Version 1.0.4
 
  * Support \b (match a word)
+ * Fix parser: support backslash in a range, eg. parse(r"[a\]x]")
 
 Version 1.0.3 (2008-04-01)
 

hachoir-regex/hachoir_regex/parser.py

     (<RegexRange '[^a-z-]'>, 7)
     >>> parseRange('[^]-]b', 1)
     (<RegexRange '[^]-]'>, 5)
+    >>> parseRange(r'[\]abc]', 1)
+    (<RegexRange '[]a-c]'>, 7)
+    >>> parseRange(r'[a\-x]', 1)
+    (<RegexRange '[ax-]'>, 6)
     """
     index = start
     char_range = []
         index += 1
     while index < len(text) and text[index] != ']':
         if index+1 < len(text) \
+        and text[index] == '\\':
+            char_range.append(RegexRangeCharacter(text[index+1]))
+            index += 2
+        elif index+1 < len(text) \
         and text[index] == '-' and text[index+1] == ']':
             break
-        if index+3 < len(text) \
+        elif index+3 < len(text) \
         and text[index+1] == '-' \
         and text[index+2] != ']':
             char_range.append(RegexRangeItem(ord(text[index]), ord(text[index+2])))