本页面有中文版。
Cette page est disponible en français.
Esta página está disponible en español.

Hunter basics

The two building blocks of every pattern: literal text and the wildcard dot — and how a pattern matches anywhere inside a string.

Read the rules: Literal characters in the Guide →Drill it: Pattern drills →

What you'll learn

Literals Matching anywhere The dot Multiple dots

Steps · 9

  1. 01Welcome, hunterBriefing
  2. 02Literal charactersExample
  3. 03Your first huntHunt
  4. 04Matches anywhereHunt
  5. 05The dot: any characterHunt
  6. 06Wildcards everywhereHunt
  7. 07More than one dotHunt
  8. 08Graduation huntHunt
  9. 09Basics completeBriefing

Regex basics: literals and the dot

A regular expression, or regex, is a short pattern that describes text. You hand it a string and it answers one question: is there a match somewhere in here? In Regex Hunter that answer decides your hunt. The pattern has to catch every target word and spare every innocent one. Regex is built into JavaScript, Python, search tools and most code editors, so what you learn here works well beyond the game.

Most patterns start with literal characters. A letter or digit in a regex stands for itself, so ink looks for an i, then an n, then a k, in that order. It doesn't care where they sit. Without extra markers a regex matches anywhere inside the string, which is why ink also finds pinky and inkwell.

The second building block is the dot. A . fills one slot with any single character: a letter, a digit, a space or a symbol. t.p fits tip, top and t-p, but never tp, because the dot always takes exactly one character. Chain a few dots to describe a fixed-length gap. These two ideas, literal text and the wildcard, are the base for every pattern you will write later.

Examples

  1. /ing/

    Catches

    • sing
    • ringer
    • kingdom

    Spares

    • sign
    • grin

    A literal matches its letters in order, at the start, middle or end of a word.

  2. /b.g/

    Catches

    • bag
    • bug
    • debug

    Spares

    • bang
    • gab

    The dot stands in for exactly one character between b and g, so bang, with two in between, is spared.

  3. /r..d/

    Catches

    • road
    • bread
    • rinds

    Spares

    • red
    • rapid
    • dark

    Two dots mean exactly two characters, so the r and d must sit three places apart.

  4. /3\.5/

    Catches

    • 3.5
    • v3.5.1

    Spares

    • 305
    • 35

    A backslash turns the dot back into a plain full stop.

Common mistakes

  • Forgetting that a pattern matches anywhere. art also catches start and party, so check the longer words you meant to spare.
  • Treating `. as a full stop. It matches any character, so 1.5 also fits 125 and 1x5; write \.` when you mean a real dot.
  • Expecting case not to matter. apple misses Apple because regex is case-sensitive by default; add the i flag to ignore case.

Read the rules: Literal characters in the Guide →Drill it: Pattern drills →