Welcome, Guest. Please Login
Tinderbox
  News:
IMPORTANT MESSAGE! This forum has now been replaced by a new forum at http://forum.eastgate.com and no further posting or member registration is allowed. The forum is still accessible via read-only access for reference purposes. If you wish to discuss content here, please use the new forum. N.B. - posting in the new forum requires a fresh registration in the new forum (sorry - member data can't be ported).
  HomeHelpSearchLogin  
 
Pages: 1
Send Topic Print
Regex and agents (Read 2931 times)
Raffael
Full Member
*
Offline



Posts: 2

Regex and agents
Aug 26th, 2013, 7:16am
 
I am working on a routine for importing notes stored in DayOne. While being a very nice app for recording notes, it is only possible to bulk export all notes. However, thanks to the explode feature of tinderbox, it is easy to create individual notes from the plain the text file DayOne exports to.
But here's the challenge. Tags in Day One are separated by a comma and are structured like this:
"Tags: research, interesting idea, follow up"
I created a prototype "Note" in Tinderbox with a custom (set) attribute "Tags". Now I want an agent that harvests the tags contained in the body of the note and puts them in the tags attribute field. Because the tag separator is a comma rather than a colon, I cannot simply match (with Regex) the entire line following "Tags: ", but have to grab every keyword individually. Any hints on how this may be achieved most efficiently?
Back to top
 
 
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Regex and agents
Reply #1 - Aug 26th, 2013, 10:03am
 
There are a number of missing parameters in the data so far, so I'll guess some of the assumptions:
  • All tags are in a single paragraph in $Text - i.e. there are no line breaks in the tags section,
  • The tags data is not the first or last paragraph of $Text, i.e there will paragraphs before and after the tag data.
  • There may be zero or or more tag values per note. In other words, the tags line may just be "Tags: [line break]".
  • No individual tag value contains a comma.
  • No individual tag value contains a semi-colon
Regex are very precise therefore you need to think about not only what you expect to get, but also possible unexpected matches (due to insufficiently precise regex).

A simple text shows this works:-

$AgentQuery:   $Text.contains("Tags: (.*?\n)")
$AgentAction:   $Tags = $Tags + ($1.replace(", ",";"));

I've not tested edge cases so do test with more (appropriate) data before simply using it in you main document.

The first regex, (.*?\n), uses a non-greedy match ahead and looks—after a line starting "Tags: "—for zero or more characters of any type up to, but not including, the first line break in the succeeding text.

Edit: corrected missing closing parenthesis in example action.
Back to top
 
« Last Edit: Aug 27th, 2013, 11:11am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Raffael
Full Member
*
Offline



Posts: 2

Re: Regex and agents
Reply #2 - Aug 27th, 2013, 4:55am
 
Thanks a lot Mark, that works beautifully in my test. There's just one bracket missing I found out. The replacement expression reads

Code:
$AgentAction:   $Tags = $Tags + ($1.replace(", ",";")) 



just in case somebody wants to use it and wonders why it doesn't work.

Best,
Raffael
Back to top
 
« Last Edit: Aug 27th, 2013, 4:56am by Raffael »  
  IP Logged
Mark Anderson
YaBB Administrator
*
Offline

User - not staff!

Posts: 5689
Southsea, UK
Re: Regex and agents
Reply #3 - Aug 27th, 2013, 11:12am
 
Oops. I've corrected the original example above for later readers. I should have copy/pasted from my test TBX!

Incidentally, the parentheses around the $1.replace() may not be needed but I put them there to make it plain to TB that I want it to do the replace action before $1 is added to $Tags.
Back to top
 
« Last Edit: Aug 27th, 2013, 11:15am by Mark Anderson »  

--
Mark Anderson
TB user and Wiki Gardener
aTbRef v6
(TB consulting - email me)
WWW shoantel   IP Logged
Pages: 1
Send Topic Print