| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛

```>>> a="\'"
>>> a
"'"
>>> a="\\\'"
>>> a
"\\'"
>>> a="\\\\'"
>>> a
"\\\\'"
>>> a="\\\\\'"
>>> a
"\\\\'"
>>> a="\\\\\\'"
>>> a
"\\\\\\'"
>>> a="\\\\\\\'"
>>> a
"\\\\\\'"
>>> a="\\\\\\\\'"
>>> a
"\\\\\\\\'"
```

[此贴子已经被作者于2019-8-16 17:45编辑过]

```>>> print('\\')
\
>>> print('\\\\')
\\
>>> print('\\\\\\')
\\\
```

re.sub(r'\\','\\\\',a) 第二个参数少一个r。re.sub(r'\\',r'\\\\',a)

re.sub(pattern, repl, string, count=0, flags=0)

```>>> a='Morton\\\'s'
>>> print (a)
Morton\'s

>>> b=re.sub(r'([\\]\')',r'\\\\\'\j\?\.',a)
>>> print(b)
Morton\\\'\j\?\.s

#在pattern里,\\会解释成\,\'会解释成' ,但是在repl里,如果使用r'' , 表达式会把\\解释成一个\,\'解释成\',\j解释成\j.
#所以我的问题可以用b=re.sub(r'([\\]\')',r'\\\'',a)来解决

>>> c='\'\j\?\.'
>>> print (c)
'\j\?\.
>>> b=re.sub(r'(\\\')','\'\j\?\.',a)
>>> print(b)
Morton'\j\?\.s
----------------------------------
>>> c='\\\'\j\?\.'
>>> print (c)
\'\j\?\.
>>> b=re.sub(r'(\\\')','\\\'\j\?\.',a)
>>> print (b)
Morton\'\j\?\.s
-----------------------------------
>>> c='\\\\\'\j\?\.'
>>> print (c)
\\'\j\?\.
>>> b=re.sub(r'(\\\')','\\\\\'\j\?\.',a)
>>> print (b)
Morton\'\j\?\.s
-----------------------------------
>>> c='\\\\\\\'\j\?\.'
>>> print (c)
\\\'\j\?\.
>>> b=re.sub(r'(\\\')','\\\\\\\'\j\?\.',a)
>>> print (b)
Morton\\'\j\?\.s
------------------------------------

```

[此贴子已经被作者于2019-8-19 10:58编辑过]

• 4
• 1/1页
• 1