AppleScript Text Delimiter for most recent text

applescripttext;

I have a lot of text on the file My_Note_Backup.txt

Each time I run an another script, it added to this text something like

demo
demo
{
demo demo demo
demo
}
demo ...

and this repeats around 60 times a day.

How can I have an AppleScript which copies only the last note in between { and }

I have the text delimiter here, but how can I get the most recent text only?

set theName to ""
set theName to the clipboard

set theText to Unicode text
set theSource to theName
property leftEdge1 : "{"
property rightEdge2 : "}"
try
    set saveTID to text item delimiters
    set text item delimiters to leftEdge1
    set classValue to text item 2 of theSource
    set text item delimiters to rightEdge2
    set theName to text item 1 of classValue
    set text item delimiters to saveTID
    theName
end try
set the clipboard to theName

the text file is

set dataBackupFile to (path to desktop folder as text) & "My_Note_Backup.txt"

Best Answer

There are a number of options to enable you to run a regex search in Applescript, which is probably the easiest way to do what you're looking for. The below code is written using Satimage, but you can also use any of the other options with the same regex string.

set regexStr to "(?<={\n)[^{}]*(?=\n}[^}]*\Z)"
find text regexStr in dataBackupFile with regexp and string result

Given the text you provided, this would return:

demo demo demo
demo demo

For completeness sake, I'll just break down the regex:

(?<={\n)[^{}]*(?=\n}[^}]*\Z)
(?<={\n)                     - makes sure the match is immediately preceded by {\n
        [^{}]*               - matches as many non-brackets as it can (your string)
              (?=\n}[^}]*\Z) - makes sure the match is immediately followed by a
                               close bracket, with no more brackets between it and
                               the end of the file

Edit: I just noticed that your example text has been edited by someone else since you first posted. If you actually meant that it's all on one line separated by spaces, you should replace the \ns with spaces.