How to convert regexp to python from perl

in Perl:

 if ($test =~ /^id\:(.*)$/ ) {

 print $1;

 }
+3
source share
4 answers

In Python:

import re

test = 'id:foo'

match = re.search(r'^id:(.*)$', test)
if match:
    print match.group(1)

In Python, regular expressions are available through the library re.

rbefore the string indicates that it is a string literal of the string , which means that backslashes are not specially processed (otherwise, any backslash must be escaped with another backslash so that the literal backslash breaks into the regular expression string).

re.search , Perl =~. re.match, , , ( Perl "" ). . .

, :, .

+15
match = re.match("^id:(.*)$", test)
if match:
    print match.group(1)
+4

Perl regex Python, Perl () Python re. , -:

def convert_re (perl_re, string_var='column_name', 
                test_value=None, expected_test_result=None):
    '''
        Returns Python regular expression converted to calls of Python `re` library
    '''

    match = re.match(r"(\w+)/(.+)/(.*)/(\w*)", perl_re)

    if not match:
        raise ValueError("Not a Perl regex? "+ perl_re)

    if not match.group(1)=='s':
        raise ValueError("This function is only for `s` Perl regexpes (substitutes), i.e s/a/b/")

    flags = match.group(4)

    if 'g' in flags:
        count=0     # all matches
        flags=flags.replace('g','') # remove g
    else:
        count=1     # one exact match only

    if not flags:
        flags=0

    # change any group references in replacements like \2 to group references like \g<2>
    replacement=match.group(3)
    replacement = re.sub(r"\$(\d+)", r"\\g<\1>", replacement)

    python_code = "re.sub(r'{regexp}', r'{replacement}', {string}{count}{flags})".format(
                    regexp=match.group(2)
                ,   replacement=replacement
                ,   string=string_var
                ,   count=", count={}".format(count) if count else ''
                ,   flags=", flags={}".format(flags) if flags else ''
            )

    if test_value:
        print("Testing Perl regular expression {} with value '{}':".format(perl_re, test_value))
        print("(generated equivalent Python code: {} )".format(python_code))
        exec('{}=r"{}"; test_result={}'.format(string_var, test_value, python_code))
        assert test_result==expected_test_result, "produced={} expected={}".format(test_result, expected_test_result)
        print("Test OK.")

    return string_var+" = "+python_code

print convert_re(r"s/^[ 0-9-]+//", test_value=' 2323 col', expected_test_result='col')

print convert_re(r"s/[+-]/_/g", test_value='a-few+words', expected_test_result='a_few_words')
0

Source: https://habr.com/ru/post/1780745/


All Articles