Skip to content

Commit 5971827

Browse files
committed
:fix: Correct #24. Problem with backslash
1 parent 4f36e28 commit 5971827

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

pyformlang/regular_expression/regex_reader.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def _pre_process_regex(regex: str) -> str:
227227
if not previous_is_escape and (current_c in SPECIAL_SYMBOLS) and \
228228
pos != len(regex) - 1 and regex[pos + 1] != " ":
229229
res.append(" ")
230-
previous_is_escape = current_c == "\\"
230+
previous_is_escape = current_c == "\\" and not previous_is_escape
231231
pos += 1
232232
return "".join(res)
233233

pyformlang/regular_expression/tests/test_python_regex.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ def _test_compare(self, regex, s_test):
203203
self.assertEqual(r_python.fullmatch(s_test) is not None, r_pyformlang.accepts(s_test))
204204

205205
def test_backslash(self):
206-
self._test_compare(".*", "]")
207206
self._test_compare(".*", "\\")
207+
self._test_compare(".*", "]")
208208

209209
def test_escape_dot(self):
210210
self._test_compare("\\.", ".")
@@ -246,7 +246,7 @@ def test_brackets_backslash_middle(self):
246246
self._test_compare(r"[a\b]", "\\b")
247247
self._test_compare(r"[a\b]", "\\")
248248

249-
def test_backslash(self):
249+
def test_backslash2(self):
250250
self._test_compare(r"\t", "t")
251251
self._test_compare(r"\t", "\t")
252252
self._test_compare(r"\t", "\\t")

pyformlang/regular_expression/tests/test_regex.py

+4
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,7 @@ def test_backslash_b(self):
290290
self.assertTrue(Regex("( a | \b )").accepts("\b"))
291291
self.assertTrue(Regex("( a | \b )").accepts("a"))
292292
self.assertFalse(Regex("( a | \b )").accepts("b"))
293+
294+
def test_backslash(self):
295+
self.assertTrue(Regex("(\\\\|])").accepts("\\"))
296+
self.assertTrue(Regex("(\\\\|])").accepts("]"))

0 commit comments

Comments
 (0)