Tinderbox Forum

Tinderbox Training Video - Exploding Notes with Action Code and Regex (Part 3)

Tinderbox Training Video - Exploding Notes with Action Code and Regex (Part 3)

Level Advanced
Published Date 1/5/21
Tags Tinderbox, aTbRef, 4Cs of Knowledge Exchange, Exploding Notes, .replace, Back-references, Regular Expression, RegEx
Video Length 17:51
Video URL https://youtu.be/RYNNngvWstw
Example File TBX - L Exploding Notes (Part 3).tbx (3.5 MB)
Revision 1
TBX Version 8.9
Instructor Michael Becker

In this lesson, I take our effort to explode notes with action code to a whole new level. Building off of Part 2, I show you how to use explode notes with action code, prototypes, dot operators (specifically .replace), and regular expressions (regex). This process transforms your $Text into individual notes all in one action. This use case shows the true transformational knowledge and note curation power of Tinderbox.

In previous lessons, we explain explode, action code, prototypes, and dot operators. New for this lesson, I introduce you to regular expression. Regular expression, invented by Stephen Kleene in the 1950s, is a foundational computing syntax used for matching text strings.

Also in this lesson, I show you how to use stamps to experiment with action code before you put it into practice. This services to purpose, 1) you can test the code before you do it on your entire file through an agent, rule, or edict, and 2) you can debug your efforts step by step to learn what’s going on in Tinderbox and generally how all of this works (aka an exploratory method of learning).

TIP: Tinderbox is not the place to learn regex, but it is an exceptional place to apply it. The best way to learn regex is through computing web sites, regex reference guides, or through software programs like Expressions or BBEdit. The reason for this simple, Tinderbox leverages regex extensively but does not provide regex visualization or editing tools, helpers, or error code management. It other words, it does not help you learn regex and won’t tell you how to fix your regex matching patterns if there are errors. There are tons of tools out there that do this and getting regex right given its dialects and your text is a trial and error exercise anyway, so there is little value to have the Tinderbox team spend their precious development resources reinventing the wheel.

As we explain in the lesson, there are a few things to take note of about regex, such as 1) most characters match the same character, e.g. an “a” matches within “a”, 2) a few “special characters” have a special meaning, like parentheses, curly brackets, and the like; if you use one or more of these, as we do in this example, you need to escape them with an , 3) a period matches any character, 4) an asterisk matches zero or more copies of whatever precedes it, 5) .* matches anything, 6) \n represented a paragraph mark, and so on. Again, there are hundreds of sites and guides on the Internet and in your local or online bookstore or library to learn about regex. Remember, regex is like any other language. It has its own syntax, terminology, grammar, and dialects. Tinderbox speaks the Perl dialect of regex.

TBX - L Exploding Notes RegExCover (Part 3)

Breaking down the regex (Part 3)

In this exercise we use the following regular expression, ([^(]+) *(([^(]+)).

Regex Description
( ) ( typically helps set off the beginning and end of a group to be matched. The value of a matched group is stored in the group, aka a back-reference. Back-reference values are stored in $ variables, e.g. $1, $2,…$9†. The group value can be used later action code expression.
[ ] Match any of the characters between the brackets
Start at the begging of the line
\ Escape key for special regex characters
+ Capture one or more characters


Figure: RegEx example with BBEdit Pattern Playgroud-captures to groups the entire line and company name


Figure: RegEx example with BBEdit Pattern Playgroud-captures to groups the entire line and full name


Figure: RegEx explanation

†Tinderbox supports up to 9 back-references, $1~$9. Tinderbox supports the $0, like BBEdit, but remember the original attribute, that is unless you change it as part of the action, is the same thing as $0.

Breaking down the action code (Part 3)

When we take are regular expressions, combine them with action code, the magic happens.

Action Code Explanation

The actions taken here will explode the text, parse the line, and place the correct values in $Name, $FullName, Organization, and clear the text.

The first action is the explode itself, it copies each respective line in the $Text to $Name, after it processes the line with the specified explode action.

Here is the second action code expression,

if($Name.contains("([^\(]+) *\(([^\(]+)\)")){ $Organization = $2; $Name = $1; $FullName = $1;

It has a lot going on.

First, we have a conditional statement, if this then that. The conditional evaluates and matches the $Name with the regex. Since the regex has groups (.e.g. pattern matches enchased in ( )) the regex values are placed in back-references. The then part, the part in between the curly brackets { }, takes the back-reference values and places them in to three attributes, $Organization, $Name, and $FullName.

The final action code, $Text="": cleans up the final, it clears the clears the $Text.

Reference materials

Note: BBEdit offers a 30day free trial for all its features, after which you must buy the licensed version to use features like the Pattern Playground. As noted, there are also free tools on the web.

Lesson Rating

We could use your feedback, please rate this lesson. Also, if you have any other feedback, please provide it in the comments. Thanks.
Please rate this lesson, did you find it useful?

    • :grinning:
    • :neutral_face:
    • :confused:

0 voters

Mark Anderson (Exploding Notes Acknowledgement)

Mark is an avid Tinderboxer and the author and maintainer of A Tinderbox Reference File (aTbRef).

Mark has been instrumental in teaching me regex and helped (well mostly did) author the regex for this training.

Hi Michael, thanks a ton for your work and videos! Really helpfull to move forward with Tinderbox.

It seems something is wrong with the download link of the example file. (or is it just me?)
Could you please look into this?

Thanks!
Erwin

:slight_smile: you are welcome.

Yup, you are are right. I did not escape the | with a . Linked fixed.