Escaping the XML Angle Bracket within a Regex

1.9k views Asked by At

I've been using the ICSharp TextEditor control, and this uses an XML file to work out the different syntax highlighting sections (strings, comments etc.) with the C# regex engine.

So I've been using this Regex to match chord symbols (from this question):

\b[A-G](?:add|maj|j|m|\+|b|\#|sus|\d|°)*(?:\b|(?<=\#))

The problem is, the < symbol is part of XML syntax, so I substituted it with &lt, making it:

\b[A-G](?:add|maj|j|m|\+|b|\#|sus|\d|°)*(?:\b|(?&lt=\#))

The problem is, after doing this, the program crashes because of an invalid Regex. I've narrowed it down, and it's definately the &lt that's causing the problem.

So is there any other way of escaping the < angle bracket while still allowing the regex engine to use it.

2

There are 2 answers

2
Mark Byers On BEST ANSWER

The XML entity for < is &lt; and not &lt.

0
Scott Weaver On

In the first regex, the token (?<=\#) is a 'positive lookbehind', saying that at this point in the match you want to look back and see a literal pound symbol (which has been escaped, but didn't need to be, # isn't a regex metacharater).

If you intended a non-capturing group instead, what you wanted, was (?: *pattern* ), ie: (?:<=#). For the record, 'less than', 'equals' and 'pound' are all normal characters in a regex as far as I know, and do not need to be escaped.