Skip to content
Metacharacters: . \d \w \s
step 1/5

Reading — step 1 of 5

Read

~1 min readBasic Matching

Metacharacters: . \d \w \s

The first sign you have a regex (and not just strstr) is metacharacters:

AtomMatches
.any character (typically except \n)
\da digit [0-9]
\Da non-digit
\wa word char [a-zA-Z0-9_]
\Wa non-word char
\swhitespace [ \t\n\r\f]
\Snon-whitespace
\n, \t, \r, \fliteral whitespace chars
\., \\, etc.literal ., literal \

The backslash makes the next character special (or un-special, depending on what it was). When parsing \d your tokenizer must produce ONE atom of length 2, not two atoms.

Implementation: store atoms as strings of length 1 or 2. The match function takes one atom + one text char and returns bool:

python

Now c\.t matches c.t exactly, while c.t matches cat, cot, cit, etc.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…