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

Field work

Real text — receipts, logs, emails — where one pattern combines everything you learned: classes, quantifiers, anchors and groups.

Drill it: Real-world formats →

Builds on Golf techniques

What you'll learn

Prices & money Emails & hashtags Logs & status codes Dates Combining it all

Steps · 12

  1. 01Into the fieldBriefing
  2. 02Read the receiptHunt
  3. 03Scrape the emailsHunt
  4. 04Validate usernamesHunt
  5. 05Sift the logHunt
  6. 06Find the hashtagsHunt
  7. 07Match 24-hour timesHunt
  8. 08Read the changelogHunt
  9. 09Validate hex coloursHunt
  10. 10Your turnHunt
  11. 11Field golfHunt
  12. 12Field work completeBriefing

Regex on real text: logs, receipts and emails

Real text is messy. A receipt mixes item names, prices and dates; a server log packs a timestamp, a level and a message into one line; an email has headers, quotes and a signature. Using regex on real text means finding the pieces you need inside all that noise, not checking one clean value.

This is where the basics meet. A class picks the characters, a quantifier sets how many, an anchor or \b pins where a match may start or end, and a group with | lists the variations people actually write. In this track you highlight prices, emails and dates in real snippets, and validate a few formats along the way.

Practise on text that looks like your own data. Copy a few real lines, include the awkward ones, and check what your pattern lights up as well as what it misses. A pattern that works on three tidy lines often fails on the fourth.

Examples

  1. /^(ERROR|WARN)\b/

    Catches

    • ERROR disk full
    • WARN slow query

    Spares

    • INFO user saw ERROR
    • WARNING: low disk

    The ^ keeps the level at the start of the line, and \b stops WARNING from passing as WARN.

  2. /\bORD-\d{4,}\b/

    Catches

    • Order ORD-20481
    • ref ORD-1234 paid

    Spares

    • ORD-12
    • WORD-5000

    \b keeps WORD-5000 out, and {4,} asks for at least four digits.

  3. /\d+([.,]\d+)?%/

    Catches

    • save 15%
    • VAT 7.5%
    • tax 7,5%

    Spares

    • 15 percent
    • page 2

    The optional group reads 15%, 7.5% and the European 7,5% with one pattern.

  4. /^(re|fwd?):/i

    Catches

    • Re: invoice
    • FWD: menu
    • fw: slides

    Spares

    • Review: draft
    • Invoice re: May

    The i flag covers Re, RE and re, and ^ ignores a re: in the middle of a subject line.

Common mistakes

  • Matching inside longer things. \d{4} finds a year, but it also finds four digits inside a phone number or an order ID; add \b or the surrounding text to pin it.
  • Letting `. * run away. It is greedy, so \(.*\) on a line with two pairs of brackets grabs everything from the first ( to the last ); a class such as [^)]*` stops at the first one.
  • Testing only the tidy lines. Real data has extra spaces, mixed case and missing fields, so test with the ugly lines too and decide which ones you want to spare.

Drill it: Real-world formats →

Read more: Real-world validators →

Read more: Email regex: copy-ready patterns that actually work →

Read more: Regex for phone numbers: US formats, E.164, and when to stop →