Tinderbox Training Video 11 - 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 Tinderbox Training - Exploding Notes with Action Code and Regex (Part 3) - YouTube
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.

this is a great tutorial, thank you very much.

A minor comment and just in case it is useful to anyone, looking at the regex, I think the second group does not require the expression for not left paren ,e.g., ([^\(]+), it could simply be (.*)

breaking down the regex

(1) First part is group 1 which matches 1 or more characters that are not ( :

([^\(]+)

(2) next regex character is space, so #1 matches up to the space but not including it.

(3) The next regex character is \( matching the open paren

(4) Next is a group that matches any character that is not (.
This confuses me since I would expect to match any character that is not )

Thus instead of ([^\(]+) I would expect ([^\)]+)

But then I see both work and I think it is because (.*) also would work, (which does) since this expression is grabbing all it can, the not ( or ) doesn’t matter since neither of those characters are expected to appear in the text being matched.

the reason this pattern can grab all it can is that the regex ends with a pattern matching \) which anchors the whole thing.

  1. \) matches the end of the string, anchoring the end.

These tutorials are great, so much to learn and well presented. Thank you!

[by the way on the Mac the Patterns app is very nice, Patterns — krillapps ]

1 Like

Now that we have created notes for the people as well as their organizations would it make sense to also create links from the person notes to the organization notes as well as from the organization notes to the people?

I think addLink(from, to, type) could be used in the explode action code to do this but need to learn how this might work. Is there a pre-defined list/registry of types?

It seems like the next logical step to be able to answer queries like ‘who are all the people associated with the organization that I know about’ and ‘who are all the organizations associated with this person’?

Is that useful? Is there a tutorial on this? (didn’t see it in the list)

(Maybe links are not the best way to think about it and maybe instead the person and organization notes should have list attributes associated with them, not sure what is appropriate)

thanks, Frederick

Hey there, yes, this operator exists, actually there are several: linkTo(), linkFrom(), linkToOriginal(), linkFromOriginal.

As for giving you a demo of this, in the note above, you say, “Now that we have created notes for the people as well as their organizations,” but in the demo above, I don’t believe we created notes for the organizations. What we did is populate an attribute in the personal note for each person.

Here is a VERY simple example of using linkTo:
TBX L - linkTo Example.tbx (179.1 KB)
Open the file and apply the stamp to the notes and a link to link (inbound) link will be created. With this code, I’m telling Tinderbox to link to the note that has the $Name specified in the $Organization attribute, and to label it as a “Person” link.

linkTo($Organization,"Person");

Remember links are directional, inbound and outbound. When you look at the person they have an outbound link and when you look at the company there will be ain inbound link.

This is a very simple example and works, but it does not scale practically when you have thousands of notes when notes are being created automatically, and when there is more than one item in a set, e.g., $Entity (the attribute I use in other demos). For more complex use cases you’d use the base thinking and wrap around other conditional operators, like the .each(), create() and other operators.