Home >Backend Development >PHP Tutorial >phpmaster | Practicing Regular Expressions with Search and Replace
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.
Key Concepts:
b
): The b
sequence ensures whole-word matching, preventing unintended replacements within larger words.()
create groups, capturing matched text for reuse using backreferences like
,
, etc. (Note: some editors may use 1
, 2
instead).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>
Our goal is to replace <div> with <code><code><ul></ul>
without affecting words containing "div" (like "divebomb"). Use the following:
bdivb
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:
Use grouping and backreferences to wrap each <a></a>
tag within <code><li>
tags:
(<a.>)</a.>
<code><li>
$1
Exercise 3: Multiple Groupings and Attributes
class
Let's enhance the list items with id
and id
attributes. We'll extract the first word from the link text as the
(<a.>)([a-zA-Z] )</a.>
<li class="navEntry" id="">
<li class="navEntry" id="$2">$1</li>
<a></a>
This uses two groups: the entire
Exercise 4: Cleaning Up
Finally, remove the extra spaces and pipe symbols:
s |s
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>
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!