regexr icon indicating copy to clipboard operation
regexr copied to clipboard

Python regex support

Open XCanG opened this issue 7 years ago • 5 comments
trafficstars

I'm frequently using regexr for checking my futore regex patterns before put them in code, would be good if you may add Python regex syntax to your site. Since I saw other language selection I think it possible to do the same with Python. Python syntax have some advantages, but in most situations it pretty same. regex lib for python from docs

I may also help you for setup it to work. Probably for main function you need something like that:

import re
import json
def check(regex_pattern, regex_flags, text, regex_replace=None):
    # determining flags before... and check if using global flag (on it depend what need to use re.search or re.finditer function)

    regex_compiled = re.compile(regex_pattern, flags=re.IGNORECASE|re.MULTILINE)
    output = {"matched": [], "replaced": ""}
    groups_len = regex_compiled.groups

    if global_flag:
        matched = regex_compiled.finditer(text)
        for match in matched:
            groups = [] # finally it will look like group[0] have full match of regex, group[1] = captured group 1, e.t.c.
            for i in range(groups_len):
                groups.append(match.group(i)) # possible to use: groups.append(match.group(i) or "") to replace "null" into empty string if group is empty
            output["matched"].append(groups) # each object will determine one match from global flag
    else:
        matched = regex_compiled.search(text)
        groups = []
        for i in range(groups_len):
            groups.append(matched.group(i))
        output["matched"].append(groups)

    if regex_replace:
        if not global_flag:
            count = 1 # replace only 1st match
        else:
            count = 0
        replaced = regex_compiled.sub(regex_replace, text, count=count)
        output["replaced"] = replaced

    return json.dumps(output)

XCanG avatar Jan 13 '18 05:01 XCanG

We'd love to have Python support. I'm planning to open source the current version in the next few days, and at that point we'd be thrilled to have someone take the lead on adding this. Let me know if you're interested.

gskinner avatar Jun 05 '18 17:06 gskinner

Yes, I'm still interested.

XCanG avatar Jun 06 '18 19:06 XCanG

Quick note that the front-end code is now available. I'm happy to share the server side solver code if you'd like to work on this.

gskinner avatar Jun 12 '18 19:06 gskinner

Python's built-in re module is based on Perl regex syntax.

See https://github.com/gskinner/regexr/issues/273 for how this can be welcomed

zikaari avatar Jul 15 '18 06:07 zikaari

What about python's pipy regex? that support fuzzy matching - can that be added?

mccart avatar Mar 26 '23 23:03 mccart