How bold is this post? Preamble

In Stack Exchange markdown, we use ** to bold out text. For example, this markdown:

The **quick brown fox jumps over the lazy** dog.


Renders as:


  The quick brown fox jumps over the lazy dog.


Of course, we use ** to close boldness as well. So less of the answer will be bold. For example:

The **quick** brown fox jumps over the **lazy** dog.


Renders as:


  The quick brown fox jumps over the lazy dog.


However, if the boldness is not closed, it renders as not bold:

The **quick brown fox jumps over the lazy dog.


Renders as:


  The **quick brown fox jumps over the lazy dog.


If the text has a single backslash \, the boldness does not take effect, either:

The \**quick brown fox jumps over the lazy dog.**


Renders as:


  The **quick brown fox jumps over the lazy dog.**


Trailing white space results in unbolded text (note, the white space after brown is a single tab):

The** quick** brown fox jumps over the lazy dog.**


Renders as:


  The** quick** brown fox jumps over the lazy dog.**


We can also use __ for bold, too, but note that only one can be active at a time. Here's a more complicated example:

The __quick**__ brown **fox__ jumps** over__ the__ lazy **dog.


Renders as:


  The quick** brown fox__ jumps over__ the__ lazy **dog.


The question:

You may write a program or function, given ASCII text either as a String argument or on STDIN, where the only special characters are **, __, \ (for escaping) and trailing whitespace, determine how many bold characters there are. This value should be printed to STDOUT or returned from your function. You do not need to support very long strings; String length is guaranteed to be no more than 30K, which is the limit for a Stack Exchange post. 

Fine print:


Can I throw an exception / other error for one case, and return normally for the other?


No. It must be either a clear, unambiguous, non errored return value for both cases. STDERR output will be ignored.

Are spaces in between words considered bold?


Yes. **quick brown** has 11 bold characters in it.

Should the \ in \**, if bold, be counted?


No. It renders as **, so if it should be bolded it would only be 2 characters.

Be completely clear: what do you mean by how many characters?


Total characters that would render bold. This means that ** is not rendered if it transforms text, but it is rendered if it does not.
Note that it's possible to make ** be bolded in several ways, e.g. **\**** -> **.
Don't consider the possibility that some text could be converted to italics. The only markdown rule to consider is ** = bold*.

On Stack Exchange, HTML Bold works too. i.e. &lt;b>&lt;/b>


Yes, I am aware. Don't consider this case, this is normal text.

What about HTML entites? e.g. &amp;lt; -> &lt;


These also should be considered as normal text, there is no HTML entity conversion.

I've thought of an example you didn't cover above!


The rules function exactly as if the text were posted on Stack Exchange, in an answer (not a comment), except that code blocks are not considered special characters. Both the four space type and the backtick type. If you are not sure about how text should be rendered, just throw it into an answer box somewhere as a test, those are the rules you should follow.



Examples:

Input:

The **quick brown fox jumps over the lazy** dog.


Output:

35


Input:

The **quick brown fox jumps over the lazy dog.


Output:

0


Input:

The __quick**__ brown **fox__ jumps** over__ the__ lazy **dog.


Output:

18


Input:

The __quick\____ brown fox **jumps over\** the** lazy \**dog.


Output:

23


Input:

The****quick brown fox****jumps over **the****lazy** dog.


Output:

11


Standard Loopholes are banned.
rs, 107 bytes
\t/ 
(?&lt;!\\)((\*|_){2})((?=\S)(?!\2)(\\\2|.)*?)?(?&lt;=\S)\1/(\n)^^((^^\3))\3
\\(\*|_)/\t
[^\t\n]/
\n/_
\t_?/
(_*)/(^^\1)

Live demo and test cases.
This is a pretty crazy...thing.
The newest test case does not work yet. WIP...
Explanation
\t/ 

Replace tabs with spaces. They have the same character count, and tabs are used later on as a special character.
(?&lt;!\\)((\*|_){2})((?=\S)(?!\2)(\\\2|.)*?)?(?&lt;=\S)\1/(\n)^^((^^\3))\3

Replace any text of length N that should be bolded
with N newlines followed by the original text.
\\(\*|_)/\t

Replace any occurrences of a delimiter immediately preceded by a slash with a tab. This is to make sure that entries like **a\*** have a character count of 2 instead of 3.
[^\t\n]/

Remove any character that's not a tab or newline.
\n/_

Replace all the newlines with underscores.
\t_?/

Remove any tabs (which represent escaped delimiters), along with any underscores that may follow them. This is related to the above issue of character counts with escaped ending delimiters.
(_*)/(^^\1)

Replace the underscore sequence with its length. This is the character count.