You are part of a team implementing an HTML editor and have been tasked with the problem of implementing the cut/copy/paste functions. One of your goals is to preserve the formatting of selected text, even though that formatting may be determined by HTML tags outside the range of the actual selected text.
You will be provided with a block of formatted text, a starting position B, and an ending position E. Your program should output the text of the substring of that text from B (inclusive) to E (exclusive), prepending and appending formatting tags as necessary so that the output is well formed and has the same format as it had in its original position.
For the purposes of this problem, format tags consist of an opening tag (such as ``< b> "), followed by some text, followed by a closing tag (such as ``< / b> "). Opening and closing tags are paired (``< /whatever> " closes ``< whatever> ") and are considered opened between the opening tag and the closing tag. A tag may not be closed unless it is the most recent unclosed tag (e.g., ``< i> abc< b> def< / i> ghi< / b> " is illegal). A tag may not be opened if it is already open (e.g., ``< b> < b> recursive b< / b> < / b> " is illegal).