我可以在正则表达式(C#)中的模式中使用变量吗 [英] Can I use variables in pattern in Regex (C#)
问题描述
我有一些HTML文本,我需要在其中替换单词以链接到它们.例如,我有带有单词"PHP"的文本,并希望将其替换为< a href ="glossary.html#php"> PHP</a>.而且我需要替换许多单词.
我的代码:
公共结构GlossaryReplace{公共字符串词;//这里的单词,例如的PHP公共字符串链接;//这里是要替换的链接,例如lossary.html#php}public static GlossaryReplace [] Replaces = null;IHTMLDocument2 html_doc = webBrowser1.Document.DomDocument作为IHTMLDocument2;字符串html_content = html_doc.body.outerHTML;为(int i = 0; i< Replaces.Length; i ++){字符串替换=< a class = \"词汇表\"href = \""+替换[i] .link +" \>"+替换[i] .word +</a>";html_content = Regex.Replace(html_content,@"\ b" + Replaces [i] .word +"\ b",替换);}html_doc.body.innerHTML = html_content;
麻烦是-这不起作用:(但是,
html_content = Regex.Replace(html_content,@"\ bPHP \ b",某些替换项");
此代码运行良好!我无法理解我的错误!
字符串的@前缀仅适用于紧随其后的字符串,因此,在连接字符串时,可能必须在每个字符串上使用它.
更改此:
html_content = Regex.Replace(html_content,@"\ b" +替换[i] .word +"\ b",替换);
收件人:
html_content = Regex.Replace(html_content,@"\ b" +替换[i] .word + @"\ b",替换);
在正则表达式中 \ b
表示单词边界,但在字符串中表示退格字符(ASCII 8).如果您使用的转义代码不存在于字符串中(例如 \ s
),但在这种情况下则不会,因为该代码同时存在于字符串和正则表达式中,因此会出现编译器错误.>
附带说明;动态创建正则表达式模式时有用的方法是 Regex.Escape
方法.它转义要用于模式的字符串中的字符,因此 @"\ b" + Regex.Escape(Replaces [i] .word)+ @"\ b"
可使模式均匀工作如果单词包含在正则表达式中具有特殊含义的字符.
I have some HTML-text, where I need to replace words to links on them. For example, I have text with word "PHP", and want to replace it with <a href="glossary.html#php">PHP</a>. And there are many words that I need to replace.
My code:
public struct GlossaryReplace
{
public string word; // here the words, e.g. PHP
public string link; // here the links to replace, e.g. glossary.html#php
}
public static GlossaryReplace[] Replaces = null;
IHTMLDocument2 html_doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
string html_content = html_doc.body.outerHTML;
for (int i = 0; i < Replaces.Length; i++)
{
String substitution = "<a class=\"glossary\" href=\"" + Replaces[i].link + "\">" + Replaces[i].word + "</a>";
html_content = Regex.Replace(html_content, @"\b" + Replaces[i].word + "\b", substitution);
}
html_doc.body.innerHTML = html_content;
The trouble is - this is not working :( But,
html_content = Regex.Replace(html_content, @"\bPHP\b", "some replacement");
this code works well! I can't understand my error!
The @ prefix for strings only apply to the immediately following string, so when you concatenate strings you may have to use it on each string.
Change this:
html_content = Regex.Replace(html_content, @"\b" + Replaces[i].word + "\b", substitution);
to:
html_content = Regex.Replace(html_content, @"\b" + Replaces[i].word + @"\b", substitution);
In a regular expression \b
means a word boundary, but in a string it means a backspace character (ASCII 8). You get a compiler error if you use an escape code that doesn't exist in a string (e.g. \s
), but not in this case as the code exist both in strings and regular expressions.
On a side note; a method that is useful when creating regular expression patterns dynamically is the Regex.Escape
method. It escapes characters in a string to be used in a pattern, so @"\b" + Regex.Escape(Replaces[i].word) + @"\b"
would make the pattern work even if the word contains characters that have a special meaning in a regular expression.
这篇关于我可以在正则表达式(C#)中的模式中使用变量吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!