Print the total length of all "quoted" characters
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
JavaScript (ES6), 64 bytes
f=([c,...a],i=a.indexOf(c))=>c?(~i&&i+f(a.splice(0,i+1)))+f(a):0
Try it online!
Commented
f = ( // f is a recursive function taking either the input string
// or an array of characters, split into
[c, ...a], // c = next character and a[] = all remaining characters
i = a.indexOf(c) // i = index of the 1st occurrence of c in a[] (-1 if not found)
) => //
c ? // if c is defined:
( ~i && // if i is not equal to -1:
i + // add i to the final result
f(a.splice(0, i + 1)) // remove the left part of a[] up to i (included) and
) // do a recursive call on it
+ f(a) // add the result of a recursive call on a[]
: // else:
0 // stop recursion
Brain-Flak, 100 bytes
({{<({}<>)<>(({<>(({}({})<>[({}<>)]))(){[{}()](<>)}{}}{}){(<>)})<>{}>{<>({}<<>({}<>)>)<>}<>[{}]}{}})
Try it online!
Commented
# Loop over each character in input and sum iterations:
({{
# Evaluate matching quote search as zero
<
# Move opening "quote" to right stack
({}<>)<>
# Until match or end of search string found:
# Note that the character to search for is stored as the sum of the top two entries in the right stack.
(
({
<>((
# Character to search for
{}({})
# Subtract and move next character
<>[({}<>)]
# Push difference twice
))
# Add 1 to evaluation of this loop
()
# If no match, cancel out both 1 and pushed difference to evaluate iteration as zero (keep one copy of difference for next iteration)
# (compare to the standard "not" snippet, ((){[()](<{}>)}{}) )
# Then move to other stack
{[{}()](<>)}{}
# If a match was found, this will instead pop a single zero and leave a zero to terminate the loop, evaluating this iteration as 0+1=1.
# Push 1 if match found, 0 otherwise
}{})
# If match found, move to left stack and push 0 denote end of "quoted" area.
{(<>)}
# Push the same 1 or 0 as before
)
# Remove representation of opening "quote" searched for
# The closing quote is *not* removed if there is a match, but this is not a problem because it will never match anything.
<>{}
>
# Move searched text back to left stack, evaluating each iteration as either the 1 or 0 from before.
# This counts characters enclosed in "quotes" if a match is found, and evaluates as 0 otherwise.
{<>({}<<>({}<>)>)<>}
# Remove 0/1 from stack; if 1, cancel out the 1 added by the closing "quote"
<>[{}]
# Repeat until two consecutive zeroes show up, denoting the end of the stack.
# (Because closing quotes are not removed, it can be shown that all other zeroes are isolated on the stack.)
}{}})