Level 3: Advanced · Lesson 25 of 30
Generic Extraction Strategies
Pull values out of messy text by anchoring on labels and delimiters. Learn a reusable toolkit of negated classes, lookarounds and captures for extraction jobs.
Extraction means finding useful values inside larger text: amounts in an invoice, IDs in a log, links in a message. Unlike validation, you are not checking that the whole input is correct. You are searching for every place where a value appears and returning just that value.
The most reliable extraction patterns lean on context. Values are usually next to a label, such as status=ok or Total: 120, or between delimiters, such as quotes, commas or brackets. Anchoring on that context is far more robust than trying to describe the value alone. (?<=status=)\w+ finds statuses wherever they appear, while \w+ would find every word.
Negated character classes are the workhorse of extraction. [^,]+ means 'everything up to the next comma', which splits a simple CSV line into fields. "[^"]*" grabs a quoted string, and \[[^\]]+\] grabs a bracketed tag. These classes stop exactly at the delimiter, need no laziness, and backtrack very little.
For values with a known shape, describe the shape with sensible limits. A signed decimal is -?\d+(?:\.\d+)?. A ticket ID is \b[A-Z]{2,4}-\d{3,6}\b. A web link is https?://[^\s]+, which accepts everything up to the next whitespace. Word boundaries keep shapes from matching inside longer tokens.
When a label is useful for finding the value but not wanted in the result, you have two options. Lookbehind, as in (?<=Total: )\d+, makes the full match the value itself. A capture group, as in Total: (\d+), matches the label too but lets your code read group 1. Captures work in every engine, including Go and Rust, which have no lookbehind.
Case matters for labels. Subtotal: 90 contains the letters total: 90, but the pattern Total: with a capital T will not match it. Use the i flag only when you really want both spellings, and add a word boundary such as \bTotal when a label might appear inside a longer word.
Finally, always run your pattern globally over a realistic sample and read the full list of matches. Extraction bugs usually show up as an extra match you did not expect, not a missing one.
Live tester
2 matches (JavaScript engine)
id=7 status=ok; id=8 status=failed
Examples
Numbers
/-?\d+(?:\.\d+)?/Temps -3.5, 12 and 18.25 today
Matches: “-3.5”, “12”, “18.25”
An optional sign and an optional fraction cover negative, whole and decimal readings.
Text
/"[^"]*"/Name "Ali Test", city "Demo Town"
Matches: “"Ali Test"”, “"Demo Town"”
The negated class stops at the closing quote, so each quoted value is separate.
Codes
/\b[A-Z]{2,4}-\d{3,6}\b/Fix BUG-1042 and OPS-120000, ignore TASK-77
Matches: “BUG-1042”, “OPS-120000”
Bounded letter and digit counts describe the ticket shape and skip IDs that are too short.
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: Extract only the number after the label `Total: ` (capital T).
Subtotal: 90 Total: 120 Tax: 30
Task 2: Extract each field from this comma-separated line.
Ali Test,Karachi,0300-0000000
Task 3: Extract the web links that start with `http://` or `https://`.
Visit https://example.com/docs or http://demo.test today