python3.5
正则表达式
Monat_pattern = r"(?P<Monat>[A-Z][a-z][a-z])"
Tag_pattern = r"(?P<Tag>\d\d)"
Uhrzeit_pattern = r"(?P<Uhrzeit>\d\d:\d\d:\d\d)"
Servername_pattern = r"(?P<Servername>[\w|\.]*)"
ServerID_pattern = r"(?P<ServerID>\w*\[\d*\]:\s\w*)"
联系他们:
pattern = Monat_pattern + r"\s*" +\\
Tag_pattern + r"\s*" +\\
Uhrzeit_pattern + r"\s*" +\\
Servername_pattern + r"\s*"+\\
ServerID_pattern + r"\s*
了解如果字符串中有“\”,我需要使用“\\”进行连接
我收到错误:
File "<ipython-input-115-1ca2b20ef199>", line 1
pattern = Monat_pattern + r"\s*" +\ Tag_pattern + r"\s*" +\ Uhrzeit_pattern + r"\s*" +\ Servername_pattern + r"\s*"+\\
^
SyntaxError: unexpected character after line continuation character
答案 0 :(得分:3)
您只能使用一个反斜杠:
pattern = Monat_pattern + r"\s*" +\
Tag_pattern + r"\s*" +\
Uhrzeit_pattern + r"\s*" +\
Servername_pattern + r"\s*"+\
ServerID_pattern + r"\s*"
您的实际代码还包含一个双反斜杠之后的空格,反斜杠必须是换行符之前的最后一个字符。
但是,使用(...)
括号扩展逻辑行会更好:
pattern = (
Monat_pattern + r"\s*" +
Tag_pattern + r"\s*" +
Uhrzeit_pattern + r"\s*" +
Servername_pattern + r"\s*"+
ServerID_pattern + r"\s*")
你可以在这里使用str.join()
:
pattern = r"\s*".join([
Monat_pattern, Tag_pattern, Uhrzeit_pattern,
Servername_pattern, ServerID_pattern, ''])
尾随''
可确保在\s*
字符串后使用额外的ServerID_pattern
。