What does this regex mean? I know the functionality of re.sub
but unable to figure out the 2nd part:
s = re.sub(r'\.([a-zA-Z])', r'. \1', s)
^^^^^^^
Can someone explain me the underlined part?
What does this regex mean? I know the functionality of re.sub
but unable to figure out the 2nd part:
s = re.sub(r'\.([a-zA-Z])', r'. \1', s)
^^^^^^^
Can someone explain me the underlined part?
Next time it you should mention which programming language you are using, because regular expression syntaxes are very different from one language to another. Also when using regular expressions to replace something, then usually the second argument isn't a regular expression, but just a string with a special syntax, so knowing the programming language would help with that, too.
\1
is a back reference to what the first capturing group (expression in parentheses) matched.
So \.([a-zA-Z])
matches a period followed by a letter, and that letter is captured (stored/saved/remembered) because it surrounded by parentheses and use at the place of \1
. The period and the letter is then replaced with a period, a space and that letter.
Examples:
.H
becomes . H
.
This.is.a.Test
becomes This. is. a. Test