Python正则表达式错误:错误字符
作为python的新手,我无法从我能找到的答案(,)中了解到发生了什么。我想做的是解析一些日志,如下所示:Python正则表达式错误:错误字符,python,regex,Python,Regex,作为python的新手,我无法从我能找到的答案(,)中了解到发生了什么。我想做的是解析一些日志,如下所示: error Traceback (most recent call last) <ipython-input-9-f8308b7bbe0f> in <module>() 51 r't, it\'s one hop! bp_header=,destination ip=10.0.
error Traceback (most recent call last)
<ipython-input-9-f8308b7bbe0f> in <module>()
51 r't, it\'s one hop! bp_header=,destination ip=10.0.0.(\d+\.*\d*),source ip=10.0.0.(\d+\.*\d*),source seqn'
52 r'o=(\d+\.*\d*),payload size=(\d+\.*\d*),offset size=(\d+\.*\d*),src time st'
---> 53 r'amp=(\d+\.*\d*),hop time stamp=(\d+\.*\d*),bundle type=([a-zA-Z]+)', re.VERBOSE)
54 hop = r2.match(line)
55
/home/dtn-012345/miniconda3/lib/python3.6/re.py in compile(pattern, flags)
231 def compile(pattern, flags=0):
232 "Compile a regular expression pattern, returning a pattern object."
--> 233 return _compile(pattern, flags)
234
235 def purge():
/home/dtn-012345/miniconda3/lib/python3.6/re.py in _compile(pattern, flags)
299 if not sre_compile.isstring(pattern):
300 raise TypeError("first argument must be string or compiled pattern")
--> 301 p = sre_compile.compile(pattern, flags)
302 if not (flags & DEBUG):
303 if len(_cache) >= _MAXCACHE:
/home/dtn-012345/miniconda3/lib/python3.6/sre_compile.py in compile(p, flags)
560 if isstring(p):
561 pattern = p
--> 562 p = sre_parse.parse(p, flags)
563 else:
564 pattern = None
/home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py in parse(str, flags, pattern)
854
855 try:
--> 856 p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
857 except Verbose:
858 # the VERBOSE flag was switched on inside the pattern. to be
/home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py in _parse_sub(source, state, verbose, nested)
413 start = source.tell()
414 while True:
--> 415 itemsappend(_parse(source, state, verbose))
416 if not sourcematch("|"):
417 break
/home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py in _parse(source, state, verbose)
550 if hi < lo:
551 msg = "bad character range %s-%s" % (this, that)
--> 552 raise source.error(msg, len(this) + 1 + len(that))
553 setappend((RANGE, (lo, hi)))
554 else:
error: bad character range e-( at position 4
#这就是我想要的
#[time-5.40052;node-1;line-638]注意:BundleTrace:很好!一捆收到,一跳!bp_头=,目标ip=10.0.0.3,源ip=10.0.0.1,源序号=139,有效负载大小=345,偏移量大小=345,src时间戳=5,跃点时间戳=5,捆绑类型=捆绑包
r2=re.compile(r'[时间-(\d+\.*\d*);节点-(\d+\.*\d*);行-(\d+\.*\d*)]注意:BundleTrace:好!一个bundle recep'
不是,这是一跳!bp\U头=,目标ip=10.0.0。(\d+\.*\d*),源ip=10.0.0。(\d+\.*\d*),源序列n'
r'o=(\d+\.*\d*)、有效负载大小=(\d+\.*\d*)、偏移量大小=(\d+\.*\d*)、src时间st'
r'amp=(\d++.*\d*),跃点时间戳=(\d++.*\d*),包类型=([a-zA-Z]+)',re.VERBOSE)
hop=r2。匹配(行)
但我犯了这样的错误:
error Traceback (most recent call last)
<ipython-input-9-f8308b7bbe0f> in <module>()
51 r't, it\'s one hop! bp_header=,destination ip=10.0.0.(\d+\.*\d*),source ip=10.0.0.(\d+\.*\d*),source seqn'
52 r'o=(\d+\.*\d*),payload size=(\d+\.*\d*),offset size=(\d+\.*\d*),src time st'
---> 53 r'amp=(\d+\.*\d*),hop time stamp=(\d+\.*\d*),bundle type=([a-zA-Z]+)', re.VERBOSE)
54 hop = r2.match(line)
55
/home/dtn-012345/miniconda3/lib/python3.6/re.py in compile(pattern, flags)
231 def compile(pattern, flags=0):
232 "Compile a regular expression pattern, returning a pattern object."
--> 233 return _compile(pattern, flags)
234
235 def purge():
/home/dtn-012345/miniconda3/lib/python3.6/re.py in _compile(pattern, flags)
299 if not sre_compile.isstring(pattern):
300 raise TypeError("first argument must be string or compiled pattern")
--> 301 p = sre_compile.compile(pattern, flags)
302 if not (flags & DEBUG):
303 if len(_cache) >= _MAXCACHE:
/home/dtn-012345/miniconda3/lib/python3.6/sre_compile.py in compile(p, flags)
560 if isstring(p):
561 pattern = p
--> 562 p = sre_parse.parse(p, flags)
563 else:
564 pattern = None
/home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py in parse(str, flags, pattern)
854
855 try:
--> 856 p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, False)
857 except Verbose:
858 # the VERBOSE flag was switched on inside the pattern. to be
/home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py in _parse_sub(source, state, verbose, nested)
413 start = source.tell()
414 while True:
--> 415 itemsappend(_parse(source, state, verbose))
416 if not sourcematch("|"):
417 break
/home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py in _parse(source, state, verbose)
550 if hi < lo:
551 msg = "bad character range %s-%s" % (this, that)
--> 552 raise source.error(msg, len(this) + 1 + len(that))
553 setappend((RANGE, (lo, hi)))
554 else:
error: bad character range e-( at position 4
错误回溯(最近一次调用上次)
在()
不,这是一跳!bp\ U头=,目标ip=10.0.0.(\d+\.*\d*),源ip=10.0.0.(\d+\.*\d*),源序列'
52 r'o=(\d+\.*\d*)、有效负载大小=(\d+\.*\d*)、偏移量大小=(\d+\.*\d*)、src时间st'
--->53 r'amp=(\d++.*\d*)、跃点时间戳=(\d++.*\d*)、捆绑类型=([a-zA-Z]+)',re.VERBOSE)
54跳=r2。匹配(线)
55
/编译中的home/dtn-012345/miniconda3/lib/python3.6/re.py(模式、标志)
231 def编译(模式,标志=0):
232“编译正则表达式模式,返回模式对象。”
-->233返回编译(模式、标志)
234
235 def清除():
/home/dtn-012345/miniconda3/lib/python3.6/re.py in_compile(模式、标志)
299如果不是sre_compile.isstring(模式):
300 raise TypeError(“第一个参数必须是字符串或编译模式”)
-->301 p=sre_compile.compile(模式、标志)
302如果不是(标志和调试):
303如果len(\u cache)>=\u MAXCACHE:
/编译中的home/dtn-012345/miniconda3/lib/python3.6/sre_compile.py(p,标志)
560如果是字符串(p):
561模式=p
-->562 p=sre_parse.parse(p,标志)
563其他:
564模式=无
/解析中的home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py(str、标志、模式)
854
855尝试:
-->856 p=_parse_sub(源、模式、标志和SRE_标志\u VERBOSE、False)
857除冗长外:
858#模式内的详细标志已打开。成为
/home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py in_parse_sub(源、状态、详细、嵌套)
413 start=source.tell()
414虽然正确:
-->415项结束(_解析(源、状态、详细))
416如果不是sourcematch(“|”):
417休息
/home/dtn-012345/miniconda3/lib/python3.6/sre_parse.py in_parse(源、状态、详细)
550如果高<低:
551 msg=“错误字符范围%s-%s”%(这个,那个)
-->552上升源。错误(消息,len(这个)+1+len(那个))
553 setappend((范围,(低,高)))
554其他:
错误:错误字符范围e-(在位置4处)
我相信“bundle type=([a-zA-Z]+)”周围一定有一些超级bug,但我找不到它。有人能告诉我为什么吗?:)你需要避开
[
和]
并匹配空格。休息一切都好。试试这个:
import re
line = "[time-5.40052;node-1;line-638]NOTE:BundleTrace:good! one bundle recept, it's one hop! bp_header=,destination ip=10.0.0.3,source ip=10.0.0.1,source seqno=139,payload size=345,offset size=345,src time stamp=5,hop time stamp=5,bundle type=BundlePacket"
r2 = re.compile(r'\[time-(\d+\.*\d*);node-(\d+\.*\d*);line-(\d+\.*\d*)\]NOTE:BundleTrace:good!\sone\sbundle\srecept,\sit\'s\sone\shop!\sbp_header=,destination\sip=10.0.0.(\d+\.*\d*),source\sip=10.0.0.(\d+\.*\d*),source\sseqno=(\d+\.*\d*),payload\ssize=(\d+\.*\d*),offset\ssize=(\d+\.*\d*),src\stime\sstamp=(\d+\.*\d*),hop\stime\sstamp=(\d+\.*\d*),bundle\stype=([a-zA-Z]+)', re.VERBOSE)
hop = r2.match(line)
你需要在开始时避开
[
。你也是第一行的]
——我认为他们都不会做@yui\u frank想要做的事。