Quotes in Back References

Hi,

I want to populate markdown block quotes inside $Text to a specific attribute $Qotd

The block quotes are in a regular format (and the quotation marks are in the text):

> "This is a quote."

The following code works as expected:

if($Text.contains("^> (.+)\n")){$Qotd=$1;}

and $Qotd becomes "This is a quote." (i.e. it includes the original quotation marks.)

But if I try to remove the quotation marks in the output by escaping them in the pattern with:

if($Text.contains("^> \"(.+)\"\n")){$Qotd=$1;}

then the search still works, but the back reference doesn’t and I get $Qotd=$1.

The real code traps for failure, so I know the escape syntax is correct and the pattern is being found: it’s the back reference which doesn’t work.

I know I could simply post-process $Qotd to strip the first and last character, but I’m interested to know:

Have I got the syntax wrong, or do " simply not work with back references?

Many thanks!

David

I think the mistake is actually that Tinderbox, certainly at present (i.e. for later readers, v8.9.1), simply cannot backslash-escape single or double straight quotes. Bear in mind that Tinderbox core design long predates Markdown and action code is an evolved macro system and not a coding environment from the ground up. So, coding edge-case are likely to exist (and, I suspect) not easy to retro fix. Not least because the ability to escape straight quotes was first asked for long ago and hasn’t been taken up.

In this case, I’d use the approach you intimated. Try:

if($Text.contains("^> (.+)\n")){$Qotd=$1;};
if($Qotd.contains('^".+"$')){$Qotd=$Qotd.substr(1,($Qotd.size-2));};

Note, neither .beginsWith() nor .endsWith() will match a " character - I tried! But if you wanted to test/trim separately for font-only or back-only quotes you could replace the second line above with these two tests:

if($Qotd.contains('^"[^"]+')){$Qotd=$Qotd.substr(1);};
if($Qotd.contains('[^"]+"$')){$Qotd=$Qotd.substr(0,($Qotd.size-1));};

Here’s my test doc (one note and a bunch of stamps!): trim-test.tbx (75.4 KB)

1 Like

Clarification. .beginsWith() nor .endsWith() do work in agents (and will match a "), but seemingly the operators don’t work at all as a test in if() conditional queries. I’ve reported this.

Hi Mark,

Thanks for the reply! I suspected that it was a limitation of the back reference system — and it’s not a big problem now I know about it.

I hadn’t gone as far as exploring the workaround: I knew I’d use substr, but you’ve kindly saved me from having to work out the gory details!

Thanks again,

David.

Well, you’ll find that I lifted my solution from my notes on String.substr().

You might also want to see my recently revised notes on using regex back-references. In looking at it now, I need to clarify if backreferences are included in a quoted string or not. IOW, if $Text is ā€œHello to you.ā€:

$MyString=$Text.replace("(^.+)to(.+$)","$1and$2");
$MyString=$Text.replace("(^.+)to(.+$)","$1"+"and"+"$2");
$MyString=$Text.replace("(^.+)to(.+$)", $1+"and"+$2);

all generate the string ā€œHello and youā€. So it’s flexible, but—note to self—I think it helps the learner to say so. Most ā€˜code’ usage is not intuitive before the fact (except to those with some coding experience and even then they may guess wrongly first time).

1 Like

Atbref would have been my first port of call if I’d needed to… :grinning:

1 Like

Page on back-references further updated, re quoting back-references.

1 Like

Thanks, Mark. Very helpful, as ever!