Home > Backend Development > PHP Tutorial > phpmaster | Practicing Regular Expressions with Search and Replace

phpmaster | Practicing Regular Expressions with Search and Replace

Lisa Kudrow
Release: 2025-03-02 08:38:09
Original
528 people have browsed it

Regular Expressions: Practice Makes Perfect – Search and Replace Exercises

This article provides practical exercises to improve your regular expression (regex) skills using the search and replace functionality found in most text editors and IDEs. We'll use a sample navigation code snippet to illustrate key concepts.

phpmaster | Practicing Regular Expressions with Search and Replace

Key Concepts:

  • Practice Beyond Code: Regex practice isn't limited to coding. Search and replace tools offer a convenient way to hone your skills.
  • Word Boundaries (b): The b sequence ensures whole-word matching, preventing unintended replacements within larger words.
  • Grouping and Backreferences: Parentheses () create groups, capturing matched text for reuse using backreferences like , , etc. (Note: some editors may use 1, 2 instead).
  • Multiple Groupings: Multiple groups allow for complex replacements, such as adding attributes or wrapping elements with tags.

Exercise 1: Word Boundaries

Let's start with this sample HTML navigation code:

<div> id="navigation">
 <a> href="https://www.php.cn/link/f5532381792b4aafeb9e52a68bf568de" title="All About Divebombs"></a>Divebombs>  |  
 <a> href="https://www.php.cn/link/0f0c4533ced2a79ab18a4bb3b6d1bb67" title="All About Endives"></a>Endives>  |  
 <a> href="https://www.php.cn/link/f07bdaf0e636773c9932fa54a952bb50" title="Indivisible by Zero"></a>Indivisible Numbers>  |  
 <a> href="https://www.php.cn/link/b555da9b21a5a45577bb2bfb58bcfea0" title="All About Division"></a>Divison>  |  
 <a> href="https://www.php.cn/link/ff2fd343aadac082034cc28e08000f82" title="All About Skydiving"></a>Skydiving>  |  
</div>
Copy after login
Copy after login

Our goal is to replace <div> with <code><code><ul></ul> without affecting words containing "div" (like "divebomb"). Use the following:

  • Search: bdivb
  • Replace: ul

This uses word boundaries (b) to target only whole words "div".

Exercise 2: Grouping and Backreferences

Now, let's refactor the anchor tags into list items (<code><li>). Our code (after Exercise 1) looks like this:

Copy after login
    id="navigation">
href="https://www.php.cn/link/f5532381792b4aafeb9e52a68bf568de" title="All About Divebombs">Divebombs>  |   href="https://www.php.cn/link/0f0c4533ced2a79ab18a4bb3b6d1bb67" title="All About Endives">Endives>  |   href="https://www.php.cn/link/f07bdaf0e636773c9932fa54a952bb50" title="Indivisible by Zero">Indivisible Numbers>  |   href="https://www.php.cn/link/b555da9b21a5a45577bb2bfb58bcfea0" title="All About Division">Divison>  |   href="https://www.php.cn/link/ff2fd343aadac082034cc28e08000f82" title="All About Skydiving">Skydiving>  |  

Use grouping and backreferences to wrap each <a></a> tag within <code><li> tags:

  • Search: (<a.>)</a.>
  • Replace: <code><li>$1

Exercise 3: Multiple Groupings and Attributes

classLet's enhance the list items with id and id attributes. We'll extract the first word from the link text as the

.
  • Search:(<a.>)([a-zA-Z] )</a.>
  • Replace:<li class="navEntry" id=""> <li class="navEntry" id="$2">$1</li>

<a></a>This uses two groups: the entire

tag and the first word of the link text.

Exercise 4: Cleaning Up

Finally, remove the extra spaces and pipe symbols:
  • Search: s |s
  • Replace: (Leave empty)

Result: Your code should now be a well-structured, unordered list:

<div> id="navigation">
 <a> href="https://www.php.cn/link/f5532381792b4aafeb9e52a68bf568de" title="All About Divebombs"></a>Divebombs>  |  
 <a> href="https://www.php.cn/link/0f0c4533ced2a79ab18a4bb3b6d1bb67" title="All About Endives"></a>Endives>  |  
 <a> href="https://www.php.cn/link/f07bdaf0e636773c9932fa54a952bb50" title="Indivisible by Zero"></a>Indivisible Numbers>  |  
 <a> href="https://www.php.cn/link/b555da9b21a5a45577bb2bfb58bcfea0" title="All About Division"></a>Divison>  |  
 <a> href="https://www.php.cn/link/ff2fd343aadac082034cc28e08000f82" title="All About Skydiving"></a>Skydiving>  |  
</div>
Copy after login
Copy after login

Remember to consult your text editor's documentation for specific regex syntax if needed. Practice these exercises, experiment with variations, and you'll quickly improve your regex skills!

The above is the detailed content of phpmaster | Practicing Regular Expressions with Search and Replace. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template