Back to Blog
Developer Tools

Use Regex to Extract Data From Text โ€” Dates, Prices, Emails, and More

2026-06-03 6 min read

Regex is one of the most powerful text processing tools available. Here are practical extraction patterns for common data types.

Data extraction with regex is one of the most useful skills in a developer's toolkit. Log files, API responses, scraped HTML, configuration files โ€” regex can pull out exactly what you need from any of these without writing a full parser.

The core concept: capturing groups

To extract data (rather than just test if it matches), you use capturing groups with parentheses. The content inside the group is what gets extracted:

// Extract email addresses from text
const text = "Contact us at support@example.com or sales@company.org for help.";
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}/g;

const emails = text.match(emailRegex);
// ["support@example.com", "sales@company.org"]

Extracting named data from logs

Named capturing groups (supported in all modern environments) make extraction code much more readable:

// Extract data from an Apache log line
const logLine = '192.168.1.1 - - [10/Jun/2026:12:34:56 +0530] "GET /api/users HTTP/1.1" 200 1234';

const logRegex = /^(?<ip>d+.d+.d+.d+).*"(?<method>w+) (?<path>[^s]+).*" (?<status>d+) (?<bytes>d+)$/;

const match = logLine.match(logRegex);
if (match) {
  const { ip, method, path, status, bytes } = match.groups;
  // ip: "192.168.1.1", method: "GET", path: "/api/users"
  // status: "200", bytes: "1234"
}

Extracting all matches from text

// Use matchAll for multiple matches with groups
const text = "Order #1234 placed at 10:30. Order #5678 placed at 14:15.";
const orderRegex = /Order #(d+) placed at (d{2}:d{2})/g;

const orders = [...text.matchAll(orderRegex)].map(m => ({
  id: m[1],
  time: m[2],
}));
// [{ id: "1234", time: "10:30" }, { id: "5678", time: "14:15" }]

Practical extraction examples

// Extract URLs from text
const urlRegex = /https?://[^s]+/g;

// Extract IP addresses
const ipRegex = /(?:d{1,3}.){3}d{1,3}/g;

// Extract dates in YYYY-MM-DD format
const dateRegex = /d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]d|3[01])/g;

// Extract hashtags
const hashtagRegex = /#[a-zA-Z0-9_]+/g;

Build and test extraction patterns with our Regex Tester.

regex extract data text patterns

More Articles