c# - 在标签之间查找文本并将其与标签一起替换

标签 c# .net regex

我正在使用以下正则表达式模式来查找 [code][/code] 标签之间的文本:

(?<=[code]).*?(?=[/code])

它返回这两个标签之间的任何内容,例如这:[code]return Hi There;[/code] 给我return Hi There;

我需要正则表达式方面的帮助,以将整个文本 along 替换为标签。

最佳答案

使用这个:

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)
        });

关于c# - 在标签之间查找文本并将其与标签一起替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8869134/

相关文章:

C# - 检查字符串是否包含相同顺序的另一个字符串的字符

c# - System.Reflection - 如何调用子级别类型的 getter 方法?

python - 如果出现空格而不是空格的情况,如何使用正则表达式

regex - 取出字符串部分和字符串的数字部分

.net - 如何用QTP实现巡航控制?

php - 使用自定义标签解析文本字段并转换为 HTML

c# - ODataProperties NextLink 为空

c# - 为什么 `IDictionary` 不允许您在其界面中通过键或默认值轻松获取值?

c# - ASP.NET MVC : what mechanic returns ViewModel objects?

.net - F# + MPI + 单声道 : Clustered Computing