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:
| Atom | Matches |
|---|---|
. | any character (typically except \n) |
\d | a digit [0-9] |
\D | a non-digit |
\w | a word char [a-zA-Z0-9_] |
\W | a non-word char |
\s | whitespace [ \t\n\r\f] |
\S | non-whitespace |
\n, \t, \r, \f | literal 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…