firasdib / Regex101

This repository is currently only used for issue tracking for www.regex101.com
3.26k stars 199 forks source link

Python group substitution #1974

Closed juliangall closed 1 year ago

juliangall commented 1 year ago

Bug Description

When specifying that a capturing group is included in the output, the Python code generator shows $1, when what is needed is \g<1>

Reproduction steps

Regex ([a]+)([a-z]) Test String aabaaacaaaad Substitution $1-

The output in the UI is shown correctly as aa-aaa-aaaa-

However, the code generated for Python has:

import re

regex = r"([a]+)([a-z])"

test_str = "aabaaacaaaad"

subst = "$1-"

result = re.sub(regex, subst, test_str, 0, re.MULTILINE)

Expected Outcome

The correct value for the substitution string should be:

subst = "\g<1>-"

Testing in Colab with the $1 puts the actual string "$1" into the output ("$1-$1-$1-"), and not the captured group. Using the \g form works correctly.

Browser

Chrome 108.0.5359.124

OS

MacOS Ventura 13.1

working-name commented 1 year ago

Hello @juliangall,

The code generator does not make editorial changes your input - if you type $1- it'll leave it as such. If you type up an invalid regex, it'll still be invalid in the code example as well. The code generator is meant to give a quick example on how to use said regex in code.

That being said \g<1>- is valid as a replacement string if you select Python as the active flavor/regex engine.

Please let me know if this helps.

juliangall commented 1 year ago

Thanks. I had assumed the selection of language in the Code Generator was the only option. I now see that selecting the active flavor is the way to go.