我正在使用以下正则表达式模式查找 [code]
和 [/code]
标签之间的文本:
(?<=[code]).*?(?=[/code])
它会返回在这两个标签之间的任何内容,例如:[code]return Hi There;[/code]
将给我 return Hi There;
。
我需要帮助使用正则表达式替换整个文本以及标签。
var s = "My temp folder is: [code]Path.GetTempPath()[/code]";
var result = Regex.Replace(s, @"\[code](.*?)\[/code]",
m =>
{
var codeString = m.Groups[1].Value;
// then evaluate this string
return EvaluateMyCode(codeString)
});
string matchCodeTag = @"\[code\](.*?)\[/code\]";
string textToReplace = "[code]The Ape Men are comming[/code]";
string replaceWith = "Keep Calm";
string output = Regex.Replace(textToReplace, matchCodeTag, replaceWith);
查看以下网站获取更多示例
http://www.dotnetperls.com/regex-replace
http://oreilly.com/windows/archive/csharp-regular-expressions.html
希望这能帮助你
\[code\](.*?)\[/code\]
替换为类似于<code>$1</code>
的内容,这将提取由[code][/code]
标签包装的内容,并将其包含在 -- 例如此示例中 -- <code></code>
标签中。
\[code].*?\[/code]
模式有什么问题? - Kirill Polishchuk