0
re.sub(r'.\d+.',r'.','ab.12.c')
#or
re.sub(r'\.\d+\.',r'.','ab.12.c')  

give ab.c, while

re.sub(r'.\d+.',r'\.','ab.12.c')
#or
re.sub(r'\.\d+\.',r'\.','ab.12.c')

give ab\\.c.

The former gives the result I want but shouldn't . be escaped in the regex, as in the latter?

wsdzbm
  • 3,096
  • 3
  • 25
  • 28
  • If you plan to match a literal `.`, you should escape it. Otherwise, it will match any character but a newline (`.` included) – Wiktor Stribiżew May 18 '16 at 11:35
  • See [*Regular Expression to match a dot*](http://stackoverflow.com/questions/13989640/regular-expression-to-match-a-dot) – Wiktor Stribiżew May 18 '16 at 11:35
  • @WiktorStribiżew it seems the problem is at the replace string. Why that dot shouldn't be escaped? – wsdzbm May 18 '16 at 11:38
  • 1
    Aha, that dot is in the replacement pattern, it is not in a regular expression. – Wiktor Stribiżew May 18 '16 at 11:39
  • 1
    So the replace string is a normal str and `re.sub(r'\.\d+\.',r'.','ab.12.c') ` is the correct one. I got it. Thanks. – wsdzbm May 18 '16 at 11:54
  • Assuming there _were_ a wildcard in the replacement string, what character should end up in the result? Also, isn't your first and second output the same, or am I missing some subtle difference? – tobias_k Jun 22 '16 at 09:38

1 Answers1

0

it seems the problem is at the replace string. Why that dot shouldn't be escaped? – Lee

Aha, that dot is in the replacement pattern, it is not in a regular expression. – Wiktor Stribiżew

Armali
  • 18,255
  • 14
  • 57
  • 171