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 bee开发者_运维问答n 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 <, making it:
\b[A-G](?:add|maj|j|m|\+|b|\#|sus|\d|°)*(?:\b|(?<=\#))
The problem is, after doing this, the program crashes because of an invalid Regex. I've narrowed it down, and it's definately the < 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.
The XML entity for <
is <
and not <
.
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.
精彩评论