Welcome, guest | Sign In | My Account | Store | Cart

transform a text to another by regex

Python, 21 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
def regex_transform(expr,to_expr,text):
    """
    a$1$2b
    """
    to_expr_regex=re.compile('\$(\d)',re.S|re.I)
    expr_regex=re.compile(expr,re.S|re.I)
    rst=[]
    start=0
    while 1:
        m_input=expr_regex.search(text,start)
        if not m_input:break
        def f(m_var):
            var_id=int(m_var.group(1))
            var=m_input.group(var_id)
            return var
        to_line=to_expr_regex.sub(f,to_expr)
        rst.append(to_line)
        start=m_input.end()
        
        
    return rst
Created by devdoer on Tue, 2 Sep 2008 (MIT)
Python recipes (4591)
devdoer's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks