```yaml
product: AlterLab
title: "Regex Extraction — Web Scraping Glossary | AlterLab"
comparison_context: "AlterLab is an alternative to Firecrawl, ScrapingBee, and Bright Data."
source_url: https://alterlab.io/glossary/regex-extraction
```

extraction

# Regex Extraction

Regex extraction uses regular expressions to match and capture specific text patterns — such as prices, dates, or identifiers — from raw HTML or text content.

Regular expressions (regex) are pattern-matching rules that can locate and extract specific text sequences from a larger string. In web scraping, regex is used when the data follows a predictable format that can be described as a pattern: phone numbers, email addresses, product SKUs, prices, dates, or embedded JSON objects.

Regex is most useful as a secondary extraction tool: first parse the HTML with a proper parser like BeautifulSoup or an XPath engine to narrow down to the relevant section, then apply regex to the resulting text to extract the precise value. Using regex on raw HTML directly is error-prone because HTML structure is nested and variable.

Common regex extraction patterns in scraping include extracting the content of a JavaScript variable from a `<script>` block, capturing a number embedded in a CSS class name, or finding all URLs matching a specific path pattern in an href attribute.

### What is Regex Extraction?

Regex extraction uses regular expressions to match and capture specific text patterns — such as prices, dates, or identifiers — from raw HTML or text content.

### How does AlterLab handle Regex Extraction?

Common regex extraction patterns in scraping include extracting the content of a JavaScript variable from a `<script>` block, capturing a number embedded in a CSS class name, or finding all URLs matching a specific path pattern in an href attribute.

## Examples

```
import re

# Extract a JSON object assigned to a JS variable
script_content = page.find("script", string=re.compile("window.__data__")).string
match = re.search(r"window.__data__s*=s*({.*?});", script_content, re.DOTALL)
if match:
    import json
    data = json.loads(match.group(1))
```

## Related Terms

[Data ExtractionThe process of identifying and pulling specific fields from raw web content, converting unstructured HTML into typed JSON records.](https://alterlab.io/glossary/data-extraction)[CSS SelectorA pattern used to match HTML elements by tag, class, ID, or attribute, used in web scraping to extract specific nodes from parsed HTML.](https://alterlab.io/glossary/css-selector)[XPathXML Path Language — a query syntax for selecting nodes in HTML or XML using path expressions, more expressive than CSS selectors for complex traversals.](https://alterlab.io/glossary/xpath)[Parse TreeThe hierarchical tree structure created by an HTML parser that represents the DOM elements of a web page for programmatic traversal.](https://alterlab.io/glossary/parse-tree)[BeautifulSoupA Python library for parsing HTML and XML and navigating the parse tree using CSS selectors or tag methods.](https://alterlab.io/glossary/beautifulsoup)

## Extract Regex Extraction data from any website

AlterLab returns clean, structured data from any public URL — no scraper infrastructure needed. Start free, no credit card required.

[View API docs](https://alterlab.io/docs/api/rest)

[Back to Glossary](https://alterlab.io/glossary)