Copy part of $Text to new attribute


(Johan Sandberg) #1

I have a bunch of notes with $Text wherein the actual notes come after the string “### Notes ###”. I’d like to make an agent that copies everything that comes after that string into a new attribute. Is there a way to do this?


(Mark Anderson) #2

Yes:

$MyString=$Text.split("### Notes ###").at(1);

As it involves regex I’ve tried several tests. See this file: Split-test.tbx (59.2 KB)

To re run the tests, in each of the 3 notes, right click the KA ‘MyString’ and reset to the default (empty string) value. Then select one or more notes and use the stamp ‘Split notes’.

One assumption: the ### Notes ### string occurs once in the $Text of every note on which the stamp code is used and only once such notes’ $Text.


(Johan Sandberg) #3

Does .at(1) refer to the first occurrence?

It’s strange because some notes have the split happen before the ### Notes ### string and if I change the code to .at(2) it works properly for those notes. But there is only one ### Notes ### string in each of them, so I’m not sure why it’s being split before it.

EDIT: aha, it’s actually semicolons that break this. For some reason it treats semicolon as matching the string.


(Mark Anderson) #4

For .at() see here. For the ,at() operator the count is zero-based, thus an offset value of one is the second part of the returned list. As per the assumptions, we expect to only have 2 parts when we .split() $Text.

Re the edit - aha, that makes sense. It is known issue #2287 and as yet awaiting a fix. The only workaround is to first detect any notes with semi-colon(s) in $Text and delete or replace then with something (e.g. a colon). Then use .split() as described above.


(Johan Sandberg) #5

Works perfectly after replacing the semicolons. Thanks for the help