How and where to learn RegEx?

Many people here use RegEx and it’s capabilities in Tinderbox. Can I ask for your advice on how and where you learned RegEx?
There are so many sites/sources, I get lost in them.

The best way to learn how to form and use regular expressions is to experiment with them, using interactive tools. (Just as learning to build a bookshelf is best done with some wood and a toolkit.) It also helps to get in the mindset of learning a game - because that’s what learning regular expressions (regex) is like.

I find it useful to rely an a three-prong approach:

  • An app like Expressions that show the “legal” metacharacters that can be used. Use it like a scratch pad.
  • A site like RegExr that contains more detailed information about different metacharacters and how they work in expressions.
  • ChatGPT that can build expressions to your specifications and, when asked, explain its logic and how the expression works. (Most other AIs can do the same – e.g., Gemini, etc.)

The internet is full of help on this. But focus on resources that explain what’s happening. If the software just solves the problem without explaining how the solution works, it’s not going to help you learn.

Also, remember that the more complex a regex problem is, the more likely there are numerous ways to formulate expressions that work. Ignore the purists who tell you there is always just one right way to form a regular expression – that’s not the way the world works.

9 Likes

Agree all the above. I have Expressions and also Patterns. If you are of the type that likes a print reference, see Stubblebein’s Regular Expression Pocket Reference, 2nd Edition (O’Reilly) is great for refreshing one’s memory.

A few extra points to @PaulWalters good answer. Be aware that regex implementations come in various flavo[u]rs, by which I means different computer languages/apps has subtle variations in syntax. IIRC, Tinderbox is closest to the ‘PCRE’ flavo[u]r. Don’t worry unduly in this regard. If you happen to learn/understand a task in one flavo[u]r (e.g. you found a really good example in a forum online) you might need to tweak the syntax but you have the greater part of the problem cracked. This neatly pulls out another aspect of regex. The first part is understanding the general language, i.e. terms like “one or more”, “zero or one”, “non-word character” or more gnomic like “non-greedy”. Once you get that you’re mastering the bigger part of the learning regex: with flavo[u]r uses which syntax is then easily checked. Don’t feel the need to learn every facet at first pass—regex capabilities have a lot of depth, so don’t over-burden your learning.

Three last points. Firstly, don’t be down-heartened when, after some success with copy/paste examples your own first efforts to write regex from new duly fail; this is most people’s experience. Why? Regex are very precise. They do what you encode not what you thought you encoded! Don’t think “this process is dumb”, think “How/why am I sending the wrong signals?”. Very often you are inadvertently matching much more/less than you’d assumed you were.

Secondly, once you get comfortable with regex avoid the trap of seeing regex as the answer to most text query/manipulation problems. Ofter there is simpler method by using Tinderbox’s toolbox of action code operators.

Lastly, if you’re needing to do complex regex on imported data, stop to ask if you have the option of fixing the issue upstream. IOW, if output from SomeOther.app needs lots of gnarly regex post-import, do see if SomeOther.app can do a more useful export, or consider doing some work using BBEdit in the command line between leaving SomeOther.app and entering the data into Tinderbox.

†. Don’t rush and get the bigger O’Reilly book “Mastering Regular Expressions”. It’s all good stuff but not for the beginner! The Pocket Reference, is what it says—a reference—and always close at hand on my desk as i forget all those special codes regex use.

‡. Both Tinderbox and the macOS command line support regex use, so your new-found skills can be applied outside Tinderbox too.

5 Likes

Personally, I love the “Pattern Playground” in BBEdit. I find it extremely useful.

As for how I learned it (Note: still learning), it was a lot of trying and error; I’ve spent time on StackOverflow and, more recently, have used ChatGPT—it has been beneficial in teaching me the patterns. The most helpful thing for me was having a specific pattern I needed to find in a particular use case. Over time, this has built up muscle memory.

2 Likes

Hello,
While I often have tried/managed to find programing ways around, over the years I have used resources such as:

Python doc: Regular Expression HOWTO — Python 3.13.0 documentation
Book: Amazon.com
Book: Amazon.com
Tool: MacTechnologies Consulting (914) 500-8878

2 Likes

@PaulWalters @mwra @satikusala @philippecmartin thank you very much for your valuable advice!

2 Likes