Print the total length of all "quoted" characters Rules

In this challenge, I'm going to redefine the definition of "quotes" a bit.


Quotation marks (AKA quotes) are any identical characters used in pairs in various writing systems to set off direct speech, a quotation, or a phrase. The pair consists of an opening quotation mark and a closing quotation mark, which is the same character(case-sensitive).
If there are quote-pairs overlapping each other,


If a pair nesting another, both pairs are still valid.
If a pair not nesting another, the first starting pair remains valid. The other is no longer considered as a pair.

When counting quoted characters(length of a pair of quotes),


The quotes themselves don't count.
Each pair's length is counted independently. Overlapping doesn't affect another.



Goal

Your goal is to print the total length of all valid quotes. This is code golf, therefore the code with the fewest bytes wins.

Examples

Legend:
    &lt;foo&gt;: Valid quotes
    ^    : Cannot be paired character

Input   : ABCDDCBA
`A`  (6): &lt;BCDDCB&gt;
`B`  (4):  &lt;CDDC&gt;
`C`  (2):   &lt;DD&gt;
`D`  (0):    &lt;&gt;
Output  : 12

Input   : ABCDABCD
`A`  (3): &lt;BCD&gt;
`B`  (0):  ^   ^
`C`  (0):   ^   ^
`D`  (0):    ^   ^
Output  : 3

Input   : AABBBBAAAABA
`A`  (0): &lt;&gt;    &lt;&gt;&lt;&gt; ^
`B`  (0):   &lt;&gt;&lt;&gt;    ^
Output  : 0

Input   : ABCDE
Output  : 0

Input   : Print the total length of all "quoted" characters
`r` (40):  &lt;int the total length of all "quoted" cha&gt;
`n` (14):    &lt;t the total le&gt;
`t` (15):     &lt; &gt;   &lt;o&gt;       &lt;h of all "quo&gt;
` `  (7):      ^   &lt;total&gt;      &lt;of&gt;   ^        ^
`h`  (0):        ^             ^                  ^
`e`  (8):         &lt; total l&gt;                 ^          ^
`o`  (0):            ^           ^         ^
`a`  (0):              ^            ^              ^ ^
`l`  (0):               ^ ^          &lt;&gt;
`"`  (0):                               ^      ^
`c`  (0):                                        ^    ^
Output  : 84

Input   : Peter Piper picked a peck of pickled peppers
`P`  (5): &lt;eter &gt;
`e`  (9):  &lt;t&gt;     ^      &lt;d a p&gt;           &lt;d p&gt;  ^
`r`  (0):     ^     ^
` `  (3):      ^     ^      &lt;a&gt;    &lt;of&gt;       ^
`i`  (5):        &lt;per p&gt;
`p`  (3):         &lt;er &gt;        ^       ^       ^ &lt;&gt;
`c`  (8):               &lt;ked a pe&gt;       ^
`k`  (7):                ^        &lt; of pic&gt;
`d`  (0):                  ^                 ^
Output  : 40

Input   : https://www.youtube.com/watch?v=dQw4w9WgXcQ
`h` (27): &lt;ttps://www.youtube.com/watc&gt;
`t`  (0):  &lt;&gt;            ^          ^
`/`  (0):       &lt;&gt;               ^
`w` (14):         &lt;&gt;&lt;.youtube.com/&gt;         &lt;4&gt;
`.`  (7):            &lt;youtube&gt;
`o`  (0):              ^       ^
`u`  (1):               &lt;t&gt;
`c`  (0):                     ^      ^             ^
`Q`  (8):                                  &lt;w4w9WgXc&gt;
Output  : 57
APL (Dyalog Unicode), 36 bytesSBCS



Full program. Prompts for input from stdin.

≢∊t⊣{t,←'(.)(.*?)\1'⎕S'\2'⊢⍵}⍣≡⍞⊣t←⍬


Try it online!

t←⍬ set up an accumulator t (for total)

⍞⊣ discard that in favour of string input from stdin (symbol: quote in console)

{…}⍣≡ apply the following anonymous lambda until stable (fix-point; previous ≡ next)

 ⊢⍵ on the argument

 …⎕S'\2' PCRE Search for the following, returning group 2 for each match:

  (.) any character (we'll call this group 1)
  (.*?) as few characters as possible (we'll call this group 2)
  \1 the group 1 character

 t,← update t by append that to t's current value

t⊣ discard that (the final list of no matches) in favour of t

≢ count the number of characters in that