我想我們python re拆分字符串在多個分隔符,但它尖叫關於我逃脫反斜槓字符。Python re.split()轉義反斜槓
我不知道該怎麼當我看着在蟒蛇逃脫反斜線改變,這就是我表現出是正確的......
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)
這是因爲它正在尋找你正在被轉義的正則表達式中的字符。 –
使用一個原始字符串:'r'|/| \\''和更好的替代單個字符的替代,使用字符類:'r'[/ \\]'' –
您需要''|/| \ \\\'' –