regex - Is there any Python implementation of logstash's grok functionality? -
logstash's grok string parsing tool built on top of regex, provides many patterns make string parsing jobs easier, fell in love first time used it. unfortunately, it's written in ruby, makes impossible used in python projects, i'm wondering there python implementation of grok, or there python alternative can simplify string parsing grok do?
i'm not aware on python ports of grok, functionality seems pretty straightforward implement:
import re types = { 'word': r'\w+', 'number': r'\d+', # todo: extend me } def compile(pat): return re.sub(r'%{(\w+):(\w+)}', lambda m: "(?p<" + m.group(2) + ">" + types[m.group(1)] + ")", pat) rr = compile("%{word:method} %{number:bytes} %{number:duration}") print re.search(rr, "hello 123 456").groupdict() # {'duration': '456', 'bytes': '123', 'method': 'hello'}
Comments
Post a Comment