Skip to content

Literals and classes

Characters and escapes

Most of a pattern is the text itself. The rest is the twelve characters that are not.

Most characters in a pattern stand for themselves. abc matches the letters a, b and c, in that order, and there is nothing more to it than that.

Twelve characters do not: ^ $ \ . * + ? ( ) [ ] { } |. Each one means something, and each one can be turned back into an ordinary character by putting a backslash in front of it.

A backslash also builds characters that are hard to type: \n is a newline, \t a tab, \x41 the character with hexadecimal code 41, which is a capital A.

And a backslash in front of a character that means nothing special is simply that character. \q is q. That sounds harmless, and it is the reason a typo in a pattern often does nothing visible at all.

Matcher

Every claim on the left has a pattern under it. Press one and it runs here, against the same engine that grades a task.