Conversation
Notices
-
re.sub('o', '\\', 'ooo') Second argument in !Python re.sub function should be r'ed else an error. Why!?
-
@fnielsen: the string '\\' means really '\', which is not a valid regular expression escape sequence (use r'\\' or '\\\\')
-
@deuxpi Yes, I know. But to me it doesn't make sense that the 2nd argument (the replacement) should be a regular expression...
-
@fnielsen: Perhaps you want to use "ooo".replace("o", "\\") then?
-
@fnielsen 2nd argument is not a re but can contain references to groups in the re, e.g.: re.sub('(.)(.)', r'\2\1', 'ab') → 'ba'
-
@fnielsen r'\' == '\\', r'\\' == '\\\\', and the second argument still needs backslashes escaped (because you can use thngs like \1 in it).
-
@fnielsen \\ would result in a single backslash, if you want just a backslash, you'll need four of them.
-
@fnielsen '\' is an escape character. e. g. '\n' is a new line, However, r'\n' are two characters, the backslash and the n
-
@bj Ahhh. Now I see. With \1 it makes sense to have the second argument handle backslashes.
-