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.
- 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.