diff --git a/src/fable-library-py/fable_library/reg_exp.py b/src/fable-library-py/fable_library/reg_exp.py index 5dc5f1f5c9..aef8cc9f99 100644 --- a/src/fable-library-py/fable_library/reg_exp.py +++ b/src/fable-library-py/fable_library/reg_exp.py @@ -71,7 +71,10 @@ def _options_to_flags(options: int) -> int: def create(pattern: str, options: int = 0) -> Pattern[str]: # raw_pattern = pattern.encode("unicode_escape").decode("utf-8") flags = _options_to_flags(options) - pattern = pattern.replace("?<", "?P<") + # Convert .NET named group syntax (?...) to Python (?P...). + # Only replace ?< when followed by a word character (name start), not by + # = or ! which are part of lookbehind assertions (?<=...) and (? equal expected + +[] +let ``test positive lookbehind works`` () = + let r = Regex @"(?<=A)\w+" + let text = "AB AC AD BD" + let ms = r.Matches(text) |> Seq.map (fun m -> m.Value) |> Seq.toList + ms |> equal [ "B"; "C"; "D" ] + +[] +let ``test negative lookbehind works`` () = + let r = Regex @"(? Seq.map (fun m -> m.Value) |> Seq.toList + ms |> equal [ "2"; "4" ]