A beginner climbs âMount Regexâ - Part #2.
The purpose of my posting here is to help other beginners gain a high-level understanding of regular expressions (Regex), by watching as I search for that myself.
The nitty-gritty details of operator syntax are available on many web sites and help documentation sources, so no need for me to repeat here.
I will curate the best of those, from a beginner perspective, as I find them, and I appreciate that others are mentioning their favorites here, too.
.
In Part 1 above, I posed the question, âWhat can Regex Do?â
Thatâs an essential question for a beginner, but I now realize that I should have asked a different question before that one:
âWhere does Regex operate, and what are its fundamental elements?â
A comparison will explain why this new question should be asked first.
.
If I were trying to explain Keyboard Maestro to a beginner, I would say,
âKM works on the Macintosh operating system.
âAnything that a person can do on a Mac, KM can do, plus KM can memorize and repeat actions, and make decisions with IF statements.
âKMâs elements are actions in the Mac OS.
Examples are opening programs, closing windows, entering text, and clicking buttons.
âKM is bounded by the Mac operating system.
It can do anything within the OS, nothing outside the OS, nothing against the OS.
KM can not make bacon and eggs.â
.
So, how to explain Regex to a beginner, in a similar fashion?
That is the goal of this post.
Iâll offer a first draft answer below.
But first, some observations on the video that I linked above: http://regexr.com/
It was disappointing.
Useful, but disappointing.
I expected a short tutorial about Regex, suitable for a beginner, but the video is just an overview of that web site.
It says very little about Regex.
In spite of that, I watched it three times and found added value each time.
Here are my notes:
By pointing out features of the web site, the video brought to my attention several topics in Regex that seem essential.
As I climb the learning mountain, I will watch carefully for more about these topics:
- "Number of matchesâ can be used how?
- What are âexpression flagsâ and when are they needed?
- What are âsubstitutionsâ?
The video showed me it would be most efficient to write and test Regex first on a tutorial web site that offers helpful, error messages.
Only after thorough testing, will I copy into a KM action.
I havenât done that yet, but I will start by making my Regex mistakes that way, rather than in KM editor.
The video mentioned a web site feature for personal âfavoritesâ, like personal bookmarks here on the KM forum.
And, the video pointed out a searchable database of ready-made expressions.
Combining those two features could make this climb easier than I expected.
.
Now, returning to the goal of how to explain to a beginner (like me), âWhere does Regex operate and what are its elements?â, here is my first draft answer:
Regex operates on characters in a computer (any computer, not just Macs).
Characters are letters A-Z, numbers 0-9, and all the special symbols that a computer can recognize.
Any color, any size, any language â Regex can see them all, including sets of characters, like words and sentences.
Regex elements are operators to find characters, count characters, and move characters around.
Elementary examples:
- Change âabcâ to âCBAâ.
- Format â18005551212â to be â1-800-555-1212â.
- Find middle initial of name âJohn Q. Publicâ = âQâ.
Regex doesnât do anything with images, sounds, drawings or anything in the user interface like clicking a button or moving the cursor.
.
So the main lesson Iâve learned so far is that Regex is all about characters, and only about characters.
I understand now that when a KM action depends on numbers, words, or sentences, Regex adds higher precision to the KM action.
.
In my next report, I will describe what answers Iâve found to the question, âWhat can Regex do?â
.
If anything Iâve written above is not clear, please tell me and I will continue to revise to make it more clear.
I welcome comment and critique on any of this.