问题 In this question I asked about extracting tags from arbitrary text. The solution provided worked well, but there's one edge case I'd like to handle. To recap, I'm parsing arbitrary user-entered text and would like to have any occurrence of < or > to conform to valid tag syntax. Where an angle bracket isn't part of a valid tag, it should be escaped as < or > . The syntax I'm looking for is <foo#123> where foo is text from a fixed list of entries and 123 is a number [0-9]+ . The parser: parser