Blog about Web Programming and Electronics
💬 👤
back Back to blog
WEB (HTML, CSS, PHP)

RegEx Quick Course: Search & Replace on Steroids

Modern developers often find themselves needing to update code across hundreds or even thousands of files. Doing this manually would take days – with the right tools, it takes seconds.

This article introduces readers to Regular Expressions (Regex), which are essentially “search and replace” on steroids. The guide is based on PHPStorm, but the same principles apply to VS Code and other editors.

1. What Are Regular Expressions?

Regular Expressions (Regex) are patterns that allow developers to search, match, and modify text according to specific rules.

Instead of searching for an exact word, the developer provides Regex with a pattern. It’s like searching for all houses with a red roof instead of one specific house.

2. How to Enable Regex in PHPStorm

When using Ctrl + R (Replace) or Ctrl + Shift + R (Replace in Path), the user must enable the .* icon — this activates Regex mode.

Important: Before using Replace All, always make a Git commit or copy the project folder. Regex is powerful — one mistake can break code in a way nobody will be able to fix.

3. Example 1: Find All Tables Ending With Numbers

The user wants to find all table classes that end with a number (tabela-1, tabela-2, tabela-3 ...).

Search: tabela-\d+

What does this mean?

  • \d+ means “any digit repeated one or more times”.
  • Result: PHPStorm highlights all tables from 1 to 999.

4. Example 2: Cleaning HTML Tags

The user wants to remove HTML tags but keep the content. For example, removing all <span> tags that clutter the markup.

Search: <span>(.*?)</span>
Replace with: $1

What does this mean?

  • $1 tells PHPStorm: “Remove the span tag, but keep whatever was inside it.”
  • (.*?) captures everything between the tags (non-greedy match).

5. Example 3: Add a Class to h3 Elements

The user wants to find all <h3> tags without a class and add a new class to them.

Search: <h3>
Replace with: <h3 class="your-new-class">

6. Basic Regex Sequences

Symbol Description Example
\d any digit 0–9
\w letter or digit A–Z, a–z, 0–9
. any character space, letter, symbol
\s whitespace space, tab, newline
(.*?) capture everything between tags content inside parentheses
\d+ one or more digits 1, 27, 1000 ...

7. Real-World Usage

Regex is often a lifesaver. Imagine having 3000 files with an incorrect PHP include, or needing to add loading="lazy" to every image. Doing this manually would take months. With Regex:

  1. The user enters a pattern.
  2. PHPStorm shows a preview.
  3. Replace All updates all 3000 files in seconds.

8. Email and Phone Number Validation

Regex is commonly used to check whether text follows a valid format:

  • Email: Does the string contain @ and a dot in the correct place?
  • Phone numbers: Does the string contain 3 digits, a dash, and then 6 digits?
  • Clean code: Find all <iframe> tags missing a width attribute.

Summary

Concept Description Value
Enable Regex .* icon in PHPStorm search Foundation for advanced operations
\d+ one or more digits find all numeric values
(.*?) captures content between tags keeps data when cleaning HTML
$1 reference to the first captured group preserves captured content during replacement
Backup before Replace All Git commit or folder copy protection against irreversible mistakes

By mastering Regex, the reader saves hours of work and becomes a far more productive developer.

back Back to blog