Dissecting a string using dissect patterns¶
You can actually dissect a string using a pattern by:
Parsing the pattern using
Pattern.parse()
;Using the pattern to dissect the string using
Pattern.dissect()
.
A complete example is the following:
from __future__ import annotations
from dissec.patterns import Pattern
pattern = Pattern.parse("[%{ts}] [%{level}] %{*p1}:%{&p1} %{*p2}:%{&p2}")
result = pattern.dissect(
"[2018-08-10T17:15:42,466] [ERR] ip:1.2.3.4 error:REFUSED",
)
print(result)
This script displays the following in the console:
{'ts': '2018-08-10T17:15:42,466', 'level': 'ERR', 'ip': '1.2.3.4', 'error': 'REFUSED'}
Using an append separator¶
If you are using append or append with order keys, you can optionally set
the append_separator
keyword on Pattern.dissect()
.
For example:
from __future__ import annotations
from dissec.patterns import Pattern
pattern = Pattern.parse("%{+name/2} %{+name/4} %{+name/3} %{+name/1}")
result = pattern.dissect(
"john jacob jingleheimer schmidt",
append_separator=", ",
)
print(result)
This script displays the following in the console:
{'name': 'schmidt, john, jingleheimer, jacob'}