Skip to content
Web Scraping APIs

What Are Regular Expressions (Regex) in Web Scraping?

Pim

Pim · Scrappey Research

June 8, 2026 3 min read

What Are Regular Expressions (Regex) in Web Scraping? — conceptual illustration
On this page

A regular expression (regex) is a compact pattern that describes a set of strings, used to find, match, and extract text. The pattern \d{3}-\d{4} matches a phone-number fragment like "555-1234"; [\w.-]+@[\w.-]+ matches an email-shaped string. In web scraping, regex is the right tool for pulling structured fragments - prices, dates, IDs, emails - out of text you have already extracted, and the wrong tool for parsing HTML structure itself, which selectors handle.

Quick facts

MatchesText patterns: digits, words, repetition, position
Core tokens\d digit, \w word char, . any, + one-or-more, * zero-or-more
CaptureParentheses ( ) capture the matched part for extraction
Good forPrices, dates, emails, IDs, cleaning extracted text
Bad forParsing HTML tree structure - use CSS/XPath instead

Regex basics

A regex is built from literal characters plus metacharacters that mean "a class of characters" or "a repetition." Character classes: \d is any digit, \w any word character (letter, digit, underscore), \s whitespace, and . any character; square brackets define a custom class like [A-Z]. Quantifiers control repetition: + means one or more, * zero or more, ? optional, and {2,4} a specific count range. Anchors ^ and $ tie the match to the start or end of a line. Parentheses create capture groups - the part of the match you actually want to pull out. So price:\s*\$(\d+\.\d{2}) finds "price: $19.99" and captures just "19.99".

How regex fits into web scraping

The clean division of labor is: use CSS selectors or XPath to navigate the HTML and grab the right element, then use regex on that element's text to extract the precise value. You select the .price span, then regex pulls the number out of "Only $19.99 today!". Regex also shines at cleanup - stripping whitespace, normalizing phone formats, splitting a combined string - and at scanning free-form text where there is no element boundary at all, like finding every email or URL in a blob of page copy. Used this way, on text rather than on markup, regex is fast, dependency-free, and exactly the right tool.

When not to use regex

The classic mistake is trying to parse HTML structure with regex - matching tags, walking nested elements, extracting an attribute by pattern-matching the raw markup. HTML is not a regular language: it nests arbitrarily, tags can be malformed, attributes reorder, and whitespace varies, so a regex that works on one page silently breaks on the next. Use a real HTML parser (which builds a proper tree) for structure, and reserve regex for the leaf-level text inside the elements that parser hands you. If your regex is matching <div or href=, that is the signal to switch to a selector. Keeping regex on the text side and selectors on the structure side keeps both robust.

Code example

python
import re

text = 'Contact: [email protected] - Price: $19.99 (was $24.99)'

email = re.search(r'[\w.-]+@[\w.-]+\.\w+', text).group()   # '[email protected]'
price = re.search(r'\$(\d+\.\d{2})', text).group(1)        # '19.99' (captured)
all_prices = re.findall(r'\$\d+\.\d{2}', text)             # ['$19.99', '$24.99']

print(email, price, all_prices)

Next in Extracting and exporting data · 5 of 7

Now get the result out in a portable format.

How to Export Scraped Data to CSV and JSON (Python)

Related terms

Concept map

Concept map

How What Are Regular Expressions (Regex) connects

The terms most directly tied to this one. Hover a node to see its neighbours, click to preview, drag to rearrange.

0 terms · 0 connections
You are here · Web Scraping APIs
Building map…

Frequently asked questions

Should I use regex to parse HTML?

No. HTML nests arbitrarily and varies in formatting, so regex patterns that match markup break easily and unpredictably. Use a real HTML parser with CSS selectors or XPath to navigate structure, then apply regex only to the text inside the elements you have extracted.

What is a capture group in regex?

A capture group is a part of a pattern wrapped in parentheses, marking the substring you want to pull out of a larger match. In '\$(\d+\.\d{2})' the parentheses capture just the number, so matching '$19.99' lets you retrieve '19.99' separately from the dollar sign.

What is regex good for in web scraping?

Extracting structured fragments from text you have already selected - prices, dates, phone numbers, emails, IDs - and cleaning or normalizing that text. It is also useful for scanning free-form copy where there is no element boundary, such as finding every URL in a block of text.

What do \d, \w, and + mean in a regex?

\d matches any digit (0-9), \w matches any word character (letter, digit, or underscore), and + means 'one or more of the preceding token'. So \d+ matches one or more digits, like '42' or '1000'. These are among the most common building blocks of a pattern.

Last updated: 2026-06-08