-1

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?

Unihedron
  • 10,251
  • 13
  • 53
  • 66
user3496974
  • 145
  • 1
  • 12
  • See [reference: What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Unihedron Aug 06 '14 at 08:23

1 Answers1

1

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

RoToRa
  • 34,985
  • 10
  • 64
  • 97
  • @Unihedron The dot in the regular expression (first argument) is escaped, which is what I was talking about in my third paragraph. The second argument shouldn't be a regular expression, so the period doesn't need to be escaped. I'm not sure which language this is (I'm assuming the `r` prefix is the syntax for a regexp literal), but I've never encountered one, which uses a regular expression as the replacement argument of a replace method, so either the OP made a mistake there, or that's how that programming language (or that method) works. – RoToRa Aug 06 '14 at 09:28