The message part of each line holds key=value pairs:
user=ada action=login. Turning that into a dictionary looks like a job for the
string methods you already know. For most lines, it is.
def parse_fields(message):
return dict(pair.split("=", 1) for pair in message.split())
print(parse_fields("user=ada action=login"))
print(parse_fields('order=1043 msg="card declined"'))The first line works. The second fails, because split() cuts wherever there's
whitespace, and "card declined" has a space inside a value. The pieces
msg="card and declined" are no longer pairs.
This is the moment to change tools, and it's worth being precise about why:
| The format… | Use |
|---|---|
| is fields separated by something that never appears inside a field | split, partition, startswith |
| has quoting, optional parts, or "this or that" | a regular expression |
The line's prefix, the date, time and level, fits the first row, and it stays
with split() from the previous lesson. Only the message needs a regex.
Converting code that already works to a regex doesn't make it better. It just
makes it harder to read.
One limit to keep in mind: the pattern below picks out the key=value pairs
from the message and skips anything else. It doesn't check that the whole
message is well formed.
Describing the pattern
A regular expression describes what the text looks like, not where to cut it.
Here, a field is a word, an =, then either a quoted value or a bare one:
import re
message = 'order=1043 msg="card declined" retry=1'
# A key is a word; a value is either "anything but a quote, in quotes" or a word.
pattern = re.compile(r'(\w+)=("[^"]*"|\S+)')
print(pattern.findall(message))
# Named groups say what each part is, and finditer yields one match at a time.
pattern = re.compile(r'(?P<key>\w+)=(?P<value>"[^"]*"|\S+)')
for match in pattern.finditer(message):
print(match["key"], "->", match["value"].strip('"'))Just five pieces are doing all of that:
| Piece | Means |
|---|---|
\w+ | one or more "word" characters: letters, digits, underscore |
"[^"]*" | a quote, then any characters that are not a quote, then a quote |
\S+ | one or more non-whitespace characters |
A|B | try A, and if it doesn't match here, try B |
(?P<name>…) | a group you can read back by name |
Write patterns as raw strings, r'…'. A backslash means something to two
readers: Python reads the string literal first, and re reads the pattern
after. r tells Python to leave every backslash alone for re. Without it,
some escapes change meaning before re sees them. "\bword\b" holds two
backspace characters, not the word boundaries r"\bword\b" asks for, so it
quietly matches nothing. Others, like "\w", survive by luck, but Python warns
about them (SyntaxWarning: invalid escape sequence), because the luck may run
out.
Once a pattern has more than a couple of parts, re.VERBOSE lets you lay it out
and comment it. Whitespace in the pattern is then ignored, so you can use it for
layout:
import re
FIELD = re.compile(r"""
(?P<key>\w+) # a key: letters, digits, underscore
=
(?P<value>
"[^"]*" # either a quoted value, which may contain spaces,
| \S+ # or a bare value, up to the next whitespace
)
""", re.VERBOSE)
print({m["key"]: m["value"].strip('"') for m in FIELD.finditer('a=1 b="two words"')})Extraction, not validation
finditer finds fields wherever they are, and skips everything else. That's
deliberate here, because log messages mix fields with free text, like
card declined order=1043. But it means this pattern extracts on a best-effort
basis. It doesn't check that a message is well formed. A key that appears twice
keeps its last value, and msg="unterminated still yields a msg (the quote
alternative fails, so the bare-value one takes over). If a format had to be
strictly key=value pairs and nothing else, you'd use fullmatch on the whole
message instead. The day's last lesson shows how.
Bug hunt: the regex that ate a line
A colleague's first version used ".*" for quoted values. It passed their test.
Then a line with two quoted values came in:
import re
FIELD = re.compile(r'(?P<key>\w+)=(?P<value>".*"|\S+)')
def parse_fields(message):
return {m["key"]: m["value"].strip('"') for m in FIELD.finditer(message)}
print(parse_fields('msg="card declined"'))
print(parse_fields('query="python books" msg="index offline"'))Reproduce. The second call is the small failing input: two fields, one line.
Inspect. There's no exception, which makes this one dangerous. What's wrong
only shows in the state: msg has vanished, and query has swallowed everything
up to the last quote on the line. When a bug produces wrong output instead of a
traceback, inspect the output, and compare it with what you expected.
Narrow. Take the pattern out of the program and try it on the smallest text that shows the problem:
import re
text = '"a" "b"'
print(re.findall(r'"(.*)"', text)) # greedy: as much as possible
print(re.findall(r'"(.*?)"', text)) # lazy: as little as possible
print(re.findall(r'"([^"]*)"', text)) # no quotes allowed inside.* is greedy: it takes as much as it can while still letting the pattern
match. Here that means everything up to the last quote. .*? is its lazy
cousin. [^"]* is the most precise choice, because it says what a quoted value
actually is: characters that aren't a quote.
Pin it. A test with two quoted values on one line. It's the case the original test missed.
Fix. Use the precise version in FIELD,
r'(?P<key>\w+)=(?P<value>"[^"]*"|\S+)', and run again.
Test regexes with more than one match
Most regex bugs appear when a pattern that works on one item meets several on the same line. A test with a single quoted value would never have caught this one.
Your turn
Parse the fields
A second service writes its log messages with single quotes as well as
double ones: user='Ada Lovelace' msg="card declined". Write parse_fields so
all seven tests pass. The FIELD pattern above is a starting point, not the
answer: it knows nothing about single quotes. One test also has an = inside
a quoted value, which is exactly the kind of case that catches out a loose
pattern.
Done looks like Ran 7 tests and OK.
import re
import sys
import unittest
def parse_fields(message):
"""Turn 'a=1 b="two words"' into {'a': '1', 'b': 'two words'}."""
raise NotImplementedError("your code here")
class TestParseFields(unittest.TestCase):
def test_bare_values(self):
self.assertEqual(parse_fields("user=ada action=login"),
{"user": "ada", "action": "login"})
def test_quoted_value_with_spaces(self):
self.assertEqual(parse_fields('msg="card declined"'), {"msg": "card declined"})
def test_equals_sign_inside_quotes(self):
self.assertEqual(parse_fields('msg="x=1 failed" retry=2'),
{"msg": "x=1 failed", "retry": "2"})
def test_single_quoted_value(self):
self.assertEqual(parse_fields("user='Ada Lovelace' msg=\"card declined\""),
{"user": "Ada Lovelace", "msg": "card declined"})
def test_two_quoted_values(self):
self.assertEqual(parse_fields('query="python books" msg="index offline"'),
{"query": "python books", "msg": "index offline"})
def test_empty_message(self):
self.assertEqual(parse_fields(""), {})
def test_bare_value_ending_in_a_quote(self):
self.assertEqual(parse_fields("name=O' retry=2"), {"name": "O'", "retry": "2"})
suite = unittest.defaultTestLoader.loadTestsFromTestCase(TestParseFields)
unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)Hint 1: the idea
A value now has three shapes: a double-quoted string, a single-quoted string, or a run of non-space characters. That's alternation with three choices, and the order still matters: the most specific shapes go first. Once a value matches, the quotes have to come off, but only the pair that matched.
Hint 2: a first step
Start from the lesson's FIELD and run the tests: only the single-quote test
fails. Loop over FIELD.finditer("user='Ada Lovelace'") and print each
m["value"], to see where the lesson's pattern cuts that value short.
Show one solution
FIELD = re.compile(r"""(?P<key>\w+)=(?P<value>"[^"]*"|'[^']*'|\S+)""")
def unquote(value):
if len(value) >= 2 and value[0] == value[-1] and value[0] in "'\"":
return value[1:-1]
return value
def parse_fields(message):
"""Turn 'a=1 b="two words"' into {'a': '1', 'b': 'two words'}."""
return {m["key"]: unquote(m["value"]) for m in FIELD.finditer(message)}The pattern is in a triple-quoted raw string because it contains both kinds of
quote. unquote removes a pair of quotes only when the first and last
characters are the same quote, so a bare value such as O' is left alone.
strip("'\"") would be shorter, but it turns O' into O, eating a quote that
belongs to the value: that is what test_bare_value_ending_in_a_quote checks.
The = inside the quotes is harmless, because finditer carries on after each
match. Once msg="x=1 failed" has matched as a whole, the x=1 inside it is
never looked at again.
The order of the alternatives matters, though. Regex alternation tries left
to right and takes the first one that matches. Write the bare value first,
(\S+|"[^"]*"), and \S+ happily matches "card on its own, so
msg="card declined" silently becomes {'msg': 'card'}. Put the most specific
alternatives first.
The rest of the course's logs only use double quotes, so the analyzer keeps the
shorter FIELD from the lesson. Add a format only when a real input needs it.
What you can now do
- Decide between string methods and a regex by the shape of the format, not by habit.
- Write a pattern with character classes, alternation and named groups, and lay
it out with
re.VERBOSE. - Recognise greedy matching, and use a negated class like
[^"]*to say exactly what a value may contain.
Next: the log files get big, and reading them all into memory stops being an option.