Python: Regex Replacement

Well, usually I do not ask such questions.

Using re.sub to find and replace regular strings is simple, but how do regular expressions work in the replacement part (and not the corresponding part)?

In particular, referring to Brian Ocken's web page , which is intended to explain just that, providing a code to duplicate the same function that he was used to in Pearl, but struggled to develop on python.

import fileinput
import re

for line in fileinput.input():
    line = re.sub(r'\* \[(.*)\]\(#(.*)\)', r'<h2 id="\2">\1</h2>', line.rstrip())
    print(line)

This sub is intended to match

* [the label](#the_anchor)

and replace it with

<h2 id="the_anchor">the label</h2>

This works: but how does the script know exactly what the shortcut and anchor are? Presumably, \ 1 and \ 2 are intended to match the desired text, but how does the script know this and not think, is it possible that the leading * refers to \ 1?

+4
1

\1 \2 "". , .

, regex:

r'\* \[(.*)\]\(#(.*)\)'
       ^^^^     ^^^^

, \1 , , \2 , .

+3

All Articles