fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.93k stars 301 forks source link

[Python] Regex lookbehind #3918

Open HLWeil opened 1 month ago

HLWeil commented 1 month ago

Description

Using lookbehind patterns in regex fails for python.

Repro code

open System.Text.RegularExpressions

let positiveLookbehind = """(?<=A).*"""

Regex(positiveLookbehind)

let negativeLookbehind = """(?<!A).*"""

Regex(negativeLookbehind)

Transpile to python and run.

Expected and actual results

Regex should be created, but fails with:

Traceback (most recent call last):
  File "C:\Users\HLWei\Downloads\FableTesting\output\abc_.py", line 5, in <module>
    create(positive_lookbehind)
  ...
re.error: missing >, unterminated name at position 4

This is caused by the following code in Fable reg_exp.py:

image

Which was probably originally added because of the different Regex syntax for grouping constructs.

Related information