After installing the Padre IDE, and using it to write and run Perl scripts, we will investigate the Regex Editor tool, which is built in the Padre software, to write and test regular expression patterns. That will be the subject of today’s article. Another light article to read, so have a nice reading.
Starting the Regex Editor
The tool can be started from the Tools menu à Regex Editor.
This will open the Regex Editor window.
Using Regex Editor to Write and Test Patterns
In its basic shape, the Regex Editor window contains three text boxes: one for the search pattern, another for the text to match against the regular expression (original text), and a third for the matched text.
On the top of the window, there are five check boxes to enable/disable the following search options:
- Ignoring the letter case (i).
- Single-line matching (s).
- Multi-line matching (m).
- Extended regular expressions (x).
- Global Search (g).
On the right, there are six buttons.
These buttons could be used to insert character classes, quantifiers, escape characters, and anchors.
We want to search the quote “Be the change that you wish to see in the world.” for any occurrence of the word “The”.
First: in the Original Text textbox, type the above string. Next, write the search pattern (literal The) in the Regular Expression textbox. This should give No Match.
If you check the Ignore Case (i) checkbox, this will cause the sentence to be matched.
Creating a Bit More Complicated Patterns
Now, we need to create a pattern that matches Capital A in the beginning of the line.
First, click the Miscellaneous button, then click ^ Beginning of line.
This will insert the caret ^ in the Regular Expression textbox. Then, write the uppercase letter A. This should match a name like “Alessandro Del Piero”.
On the other hand, a name like “Raul Gonzalez” will not match that pattern.
Substituting a Matched Text
To use the substitution feature of the Regex Editor, check the Show Substitution checkbox. This will show two additional textboxes: Replace text with, and Result from replace.
We need to replace each “the” in the quote “The whole world steps aside for the man who knows where he is going.” with “THE”, ignoring case.
Okay, ignoring case means using the i option, and replacing all matches requires the global substitution (the g option).
Inserting the Written Pattern into Perl Code
When you are done with the pattern you are writing, you can ask Regex Editor to insert it into your Perl code. This is done using the Insert button.
In this article, we have learned how to use the Regex Editor tool to write and test regular expression patterns. I hope you enjoyed reading. See you.