Skip to content
DocsAll
技巧

Regular Expressions in Practice: Common Text Processing Cases

Timi Tian · Published on March 24, 2026 · Updated on July 12, 2026
Regular ExpressionsRegexText Processing

Three Practical Use Cases for Regex

In practice, regex comes down to three types of operations:

  • Validation: Check whether text matches a format (e.g., email, phone number)
  • Extraction: Find content matching a pattern in text (e.g., all URLs, all amounts)
  • Replacement: Replace parts matching a pattern with something else (e.g., unify date formats, mask phone numbers)

Let's demonstrate with practical cases.

Validation Cases

Validate a Phone Number

Regex: ^1[3-9]\d{9}$

Key points: Use ^ and $ to anchor both ends, ensuring the entire string is a phone number rather than longer text that merely contains one. Restricting the second digit to 3-9 follows current number-range rules.

Validate an Email

Regex: ^[\w.-]+@[\w.-]+.\w+$

Key points: The simplified version is good enough; a strict email regex is very complex. In real projects, use the simplified version plus a verification email for double assurance.

Validate an ID Card

Regex: ^\d{17}[\dXx]$

Key points: 18 digits, the first 17 are digits, and the last is a digit or X. This only validates the format, not the checksum.

Validate a URL

Regex: ^https?://[\w.-]+

Key points: Starts with http or https, followed by a domain. A simplified version; a complete URL regex is extremely complex.

Extraction Cases

Extract All Phone Numbers

Regex: 1[3-9]\d{9}

No ^ or $; find all phone numbers in the text. Use global matching (the g flag).

Extract All URLs

Regex: https?://[\w./?=&%-]+

Matches URLs starting with http(s). Real URLs have more complex characters; adjust the character set as needed.

Extract All Amounts

Regex: [¥$]\d+(?:.\d+)?

Matches amounts starting with ¥ or $, with an optional decimal. For example, ¥100, $99.9.

Extract HTML Tag Content

Regex: <a[^>]*>([^<]+)

Extracts the text inside an a tag. The parentheses capture the text content. Note that regex for HTML isn't robust; use a parser for complex HTML.

Replacement Cases

Mask Phone Numbers

Replace the middle 4 digits with asterisks: take 1[3-9]\d{9} and keep the first 3 and last 4.

In practice, use capture groups: regex (1[3-9]\d)\d{4}(\d{4}), replace with 1\1****\2. \1 and \2 reference the groups.

Unify Date Formats

Convert 2026/07/12 to 2026-07-12: regex (\d{4})/(\d{2})/(\d{2}), replace with \1-\2-\3.

Remove Extra Spaces

Collapse multiple consecutive spaces into one: regex \s+, replace with a single space.

Strip HTML Tags

Remove all HTML tags and keep only text: regex <[^>]+>, replace with empty. Note this may drop > characters inside tag attributes; use a parser for complex cases.

Debugging Tips

Validate Step by Step with a Testing Tool

Use the Regex Tester:

  1. Enter the regex
  2. Enter test text (include samples that should and shouldn't match)
  3. Review the highlighted matches
  4. Adjust the regex step by step until the results are correct

Match First, Optimize Later

First write a regex that matches, then consider optimization (performance, precision). Don't chase perfection from the start.

Use Online Regex Visualization

For complex regex, use a visualization tool to break down the structure and clarify what each segment means.

Watch for Edge Cases

  • Empty strings
  • Strings that only partially match
  • Strings with special characters
  • Very long strings

Reusable Regex Patterns

Chinese Characters

[\u4e00-\u9fa5]+

Postal Code

^\d{6}$

QQ Number

^[1-9]\d{4,10}$

Hex Color

^#[0-9a-fA-F]{6}$

Version Number

^\d+.\d+.\d+$

Common Pitfalls

Greedy Matching

.* matches too much. Use [^<]+ instead of .* to extract tag content and avoid matching across tags.

Unescaped Special Characters

Metacharacters like dot, star, and question mark must be escaped when matched literally. To match a backslash itself, write \.

Cross-line Matching

By default, . doesn't match newlines. To process multi-line text, enable single-line mode or use [\s\S] to match any character.

Performance Issues

Catastrophic backtracking: nested quantifiers like (a+)+ can be extremely slow on certain inputs. Watch the performance of complex regex; for very long text, truncate before matching.

Summary

Regex in practice falls into three types: validation, extraction, and replacement. Validation uses ^ and $ anchors, extraction uses global matching, and replacement uses capture group references. Test and verify your regex with a tool first, then optimize. Memorize a few common patterns (phone number, email, date, URL)—they cover everyday needs. The DocsAll Regex Tester runs in your browser; write and test on the fly without uploading any text.

T
Timi Tian 创始人 / 全栈工程师

DocsAll 创始人,10 年全栈开发经验,专注浏览器端文档处理技术与隐私保护架构。前新加坡科技公司技术负责人。