Do <
and >
need escaped in a C# regex? I'm uncertain because of named groups and because this regex cheat sheet has them listed in the metacharacters section.
html = Regex.Replace(html, "(<body.*?>)", replacement);
or
html = Regex.Replace(html, "(\<body.*?\>)", replacement);
It is never necessary to escape <
or >
in regexen. The only context in which they have a special meaning is if the <
comes after (?
. And since regular characters aren't allowed directly after (?
escaping the <
there would make no sense (ie (?\\<blabla\\>)
would be ill-formed).
If you want to match the literal sequence ?<bla>
, you'd need to escape the ?
, not the <
and in all other cases <
and >
have no special meaning.
In this expression they will not need escaped because they are not wrapped around a word . If your expression was something more like (?<body>.*?)
then what it would do is capture .*?
and name it body
.
在这种情况下,它并不是绝对必要的,但是如果你确实逃避了,你肯定清楚你的意思,而当你在6个月后审查你的代码或者其他人阅读它时,逃避可能会引起对意义的怀疑。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.