You are slightly mixing up a few string representations here. The actual regular expression (disregarding any language specific oddities) would simply be
\*(.*?)\n
(literally those 9 characters)
However, I suppose you've either been using Java or Python without raw strings. In that case, to create the above string in memory your code has to double the backslashes:
"\\*(.*?)\\n"
This is because, if you didn't double them, Python would already remove them when compiling the string. But now the string is compiled to these 9 characters again: \*(.*?)\n
. If print these out you will get (as jd. said) a display including the double backslashes. But if you call len(string)
it will say 9
, not 11
.
So you only want 9 characters. Then why write 11 in your file? If you write eleven, then upon display the backslashes will be double escaped again. But call len(input)
on the result of open
. It will say 11
, not 15
.
This is also why you should always use raw strings when defining regular expressions within your code. Then you never need any additional escapes (except for quotes):
r"\*(.*?)\n"
which will again leave you with 9 characters (because the backslashes are left untouched upon compilation of the string).