Level 1: Foundations · Lesson 2 of 30
Literal text
Literal characters match themselves, exactly and in order. This lesson shows how literal patterns behave with spaces, digits and partial words.
A literal is a character in a pattern that means only itself. Letters, digits, spaces, commas, hyphens, colons and most punctuation are literals. When you write error, the engine looks for an e, followed immediately by r, r, o and r. Nothing more and nothing less.
Order matters. The pattern ab does not match ba. Every character must appear in the same order as in the pattern, with no extra characters in between. If the text says a b with a space, the pattern ab fails, because the space is a character too and the pattern did not ask for it.
Spaces are literal characters. The pattern New York contains a real space, so it matches New York but not NewYork. This surprises many beginners who add a space by accident at the end of a pattern and then wonder why nothing matches. Watch out for trailing spaces when you copy patterns.
A literal pattern does not care about word boundaries. The pattern sun matches in sun, sunday and sunny. The pattern 10 dollars will not match inside 100 dollars, because after the 1 and 0 the text has another 0 instead of a space. Thinking character by character makes these results easy to predict.
Literal matching is case-sensitive. colour and Colour are different patterns. In every major language, including JavaScript, Python, PHP, Java, Go, C#, Rust and Ruby, the default is case-sensitive, and you opt into case-insensitive matching with a flag such as i or re.IGNORECASE.
Some characters look literal but are not. The dot, the plus, the question mark, brackets and a few others have special meanings. If you need to match a real dot or a real plus sign, you must escape it with a backslash, which is covered in the escaping lesson. For now, stick to letters, digits, spaces and simple punctuation like commas and hyphens.
Literal patterns are surprisingly useful. Finding every occurrence of an error code, a fixed prefix like INV-, or a status number like 404 needs nothing more. When a literal pattern is enough, it is also the fastest and clearest choice.
Live tester
2 matches (JavaScript engine)
error: disk full. No error found later. ERROR is a different case.
Examples
Numbers
/404/gStatus 404, then 200, then 404 again.
Matches: “404”, “404”
The literal 404 finds each status code with those exact three digits.
Text
/sun/gsunday sun sunny
Matches: “sun”, “sun”, “sun”
A literal matches inside longer words as well as on its own.
Codes
/INV-/gINV-001, INV-002, REF-003
Matches: “INV-”, “INV-”
A fixed prefix such as INV- is a good use of a literal pattern.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match the British spelling colour, not the American spelling.
colour color colours
Task 2: Match the phrase 10 dollars exactly, without grabbing just the word dollars.
price: 10 dollars, 100 dollars
Task 3: Match New York with exactly this capitalisation.
New York, york, NEW YORK, New Yorker