我试图用python重新在多个分隔符上拆分字符串,但它对我转义的反斜杠字符大喊大叫。在
我不确定要更改什么,因为当我在python中寻找转义反斜杠时,我看到的是正确的。。。在
import re
def get_asset_str(in_str):
split = re.split(' |/|\\' , in_str)
Traceback (most recent call last):
File "AssetCheck.py", line 15, in <module>
get_asset_str(line)
File "AssetCheck.py", line 4, in get_asset_str
split = re.split(' |/|\\' , in_str)
File "C:\Python27\lib\re.py", line 167, in split
return _compile(pattern, flags).split(string, maxsplit)
File "C:\Python27\lib\re.py", line 244, in _compile
raise error, v # invalid expression
sre_constants.error: bogus escape (end of line)
试试看
这应该是您想要的:
结果:
^{pr2}$需要四舍五入转义反斜杠。或者使用原始输入(我更喜欢这个,但那只是我自己)
第一个反斜杠是在字符串文本级别转义第二个反斜杠。但是正则表达式引擎需要反斜杠也转义的,因为它也是正则表达式的一个特殊字符。在
使用“原始”字符串文本(例如
r' |/|\\'
)或四倍反斜杠。在相关问题 更多 >
编程相关推荐