ntug

Four sheets, printable

Cheatsheets

The parts of regex, cron, chmod and HTTP status codes you look up twice a week. Short rows, real examples, and a small calculator wherever arithmetic is involved. Print the page and the controls disappear, leaving the tables.

ARegex (JavaScript flavour)

These rows follow the JavaScript engine, which is what the regex tester in ntug runs. Other languages differ in details: lookbehind support, named group syntax, what \w and \d include. When a pattern behaves differently somewhere else, suspect the engine before the pattern.

Characters and classes

Regex characters and classes
TokenMatchesTry
.Any character except a line break. With the s flag, line breaks too.a.c finds abc and a-c
\d   \DA digit 0 to 9, or anything that is not.\d\d finds 42
\w   \WA letter, digit or underscore (ASCII only), or anything else.\w+ finds user_01
\s   \SWhitespace such as space, tab and line breaks, or anything else.\s+ finds a run of spaces
\b   \BA word boundary, or a spot that is not one. Zero width.\bcat\b skips concat
[abc]One of the listed characters.gr[ae]y finds gray and grey
[^abc]Any character except the listed ones.[^0-9] finds a non-digit
[a-z]One character from a range. Ranges go inside brackets.[A-Fa-f0-9] is one hex digit
\.A backslash turns a special character into a literal one.1\.5 finds 1.5 only
\p{L}A Unicode letter. Needs the u flag.\p{L}+ finds naïve whole

Anchors and quantifiers

Regex anchors and quantifiers
TokenMeaningTry
^   $Start and end of the input. With the m flag, of each line.^\d+$ is digits only
*Zero or more of the previous item, as many as possible.ab* finds a, ab, abbb
+One or more.\d+ finds 2024
?Zero or one. Makes the previous item optional.colou?r
{3}Exactly three.\d{3}
{2,}Two or more.\w{2,}
{2,4}Between two and four.\d{2,4}
*?   +?   ??Lazy versions: take as little as possible.<.+?> stops at the first >

Groups and lookaround

Regex groups and lookaround
TokenMeaningTry
(abc)A capture group, numbered from 1 by its opening parenthesis.(\d+)-(\d+) gives $1 and $2
(?:abc)Group without capturing. Use it when you only need grouping.(?:ab)+
(?<year>\d{4})A named capture group, read from match.groups.year.(?<y>\d{4})-(?<m>\d{2})
\1   \k<name>Match the same text a group already matched.(\w)\1 finds the double letter in book
a|bEither side. Lowest precedence, so group it when mixing.gr(?:a|e)y
(?=x)Lookahead: followed by x, without consuming it.\d+(?=%) finds 50 in 50%
(?!x)Negative lookahead: not followed by x.\d+(?!%)
(?<=x)Lookbehind: preceded by x.(?<=\$)\d+ finds 30 in $30
(?<!x)Negative lookbehind: not preceded by x.(?<!\$)\b\d+

Flags

Regex flags
FlagNameEffect
gglobalFind every match, not just the first. In ntug’s tester.
iignore caseLetters match in either case. In ntug’s tester.
mmultiline^ and $ match at every line break. In ntug’s tester.
sdotAll. also matches line breaks. In ntug’s tester.
uunicodeTreat the pattern and text as code points; enables \p{…}. In ntug’s tester.
ystickyMatch only at the current lastIndex. Useful in tokenizers; not offered in the tester.
dindicesAlso report start and end offsets for each group. Not offered in the tester.
vunicodeSetsA stricter superset of u with set operations. Newer engines only; check support first.

Patterns worth keeping

  • ^\d{4}-\d{2}-\d{2}$ checks the shape of an ISO date. It does not know that month 13 is wrong.
  • ^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$ is a CSS hex colour, short or long.
  • \s+$ finds trailing whitespace; ^\s*$ finds blank lines (use the m flag).
  • Skip email and URL validation by regex. The rules are long enough that a pattern is either wrong or unreadable. Send a message and see if it arrives.

BCron

Classic cron has five fields, separated by spaces. Read them left to right as minute, hour, day of month, month, day of week.

Cron fields and allowed values
PositionFieldAllowed
1Minute0-59
2Hour0-23
3Day of month1-31
4Month1-12 or JAN-DEC
5Day of week0-7 or SUN-SAT; both 0 and 7 mean Sunday

Operators

  • * means every value in the field.
  • , separates a list: 1,15 is the 1st and the 15th.
  • - is a range: 9-17 is nine through seventeen.
  • / is a step: */15 is every fifteenth value, 10-40/10 is 10, 20, 30, 40.

Common schedules

Common cron schedules
ExpressionRuns
* * * * *Every minute
*/15 * * * *Every 15 minutes
0 * * * *At the top of every hour
30 2 * * *Every day at 02:30
0 9 * * 1-509:00 on weekdays
0 0 1 * *Midnight on the first of each month
0 0 * * 0Midnight every Sunday
5,35 8-17 * * *Minutes 5 and 35 of each hour from 08:00 to 17:59

Gotchas

  • When both day of month and day of week are restricted, classic cron runs when either matches, not both. 0 0 13 * 5 fires on every 13th and on every Friday.
  • Most cron daemons evaluate in the host’s time zone. Daylight saving changes can skip or repeat a local time.
  • Shortcuts such as @daily and @reboot exist in many implementations but not all. Quartz-style expressions add a seconds field and characters like ?, L and #; those are a different dialect.

Explain an expression

Cchmod

Each of owner, group and others gets three bits: read is 4, write is 2, execute is 1. Add them up per class and you have one octal digit, so rwxr-xr-x is 7, 5, 5.

Common chmod modes
ModeSymbolicTypical use
644rw-r--r--Ordinary files. You write, everyone reads.
755rwxr-xr-xScripts, programs and most directories.
600rw-------Private keys and secrets. Many tools refuse looser modes.
700rwx------A directory only you may enter.
664rw-rw-r--Files a group edits together.
400r--------Read-only for you, locked against accidents.
777rwxrwxrwxAnyone may change it. Almost never the right fix.

Worth remembering

  • On a directory, x means “may enter and reach things inside”, and r means “may list names”. A directory without x is a closed door even if its files are readable.
  • Three extra bits sit in front: setuid is 4000, setgid is 2000 and sticky is 1000. They show up in the symbolic form as s or t in the execute slot, or capital S and T when execute is off.
  • New files start from 666 and new directories from 777, then the shell’s umask removes bits. A umask of 022 gives you 644 and 755.

Mode calculator

Special bits
Symbolic
rwxr-xr-x
Command
chmod 755 file
In words

DHTTP status families

The first digit tells you who should act. 1xx: wait. 2xx: it worked. 3xx: go somewhere else. 4xx: the request was wrong. 5xx: the server failed. The hints below are the first thing worth checking, not a diagnosis.

HTTP status codes
CodeNameMeaningFirst thing to check