Regex用于替换标记

本文关键字:替换 用于 Regex | 更新日期: 2023-09-27 18:25:22

我需要替换像这样的字符串

@@colored:some_text @color:clr@@

带有以下html标签:

<p style='color:clr;'>some_text</P>

我写了一个正则表达式来搜索这样的文本片段,但我不知道如何进行替换。以下是我的regex 示例

下面是我的C#代码的一个例子,我试图在中做到这一点

    private string Colored(string data)
    {
        var colorMatches = Regex.Matches(data, "@@colored:(.|'n)*? @color:(.*?)@@");
        if (colorMatches.Count == 0)
            return data;
        var sb = new StringBuilder();
        var matches = new List<Match>();
        sb.Append(Regex.Replace(data, @"@@colored:(.|'n)*? @color:(.*?)@@", match =>
        {
            // i don't know how to replace text properly
        }));
        return sb.ToString();
    }

请帮我做文本替换。提前谢谢!

Regex用于替换标记

Regex.Replace允许您使用$<number>语法来引用正则表达式中定义的捕获组捕获的值以进行替换。您对Replace的调用将如下所示:

Regex.Replace(
    data
,   @"@@colored:((?:.|'n)*?) @color:(.*?)@@"
,   @"<p style='$2;'>$1</p>"
)

$2是指(.*?)捕获组的内容;$1是指((?:.|'n)*?)的内容。请注意,在不创建捕获组的情况下,使用非捕获括号(?: ...)进行分组。不过,由于回溯,这可能会导致显著的速度减慢,因此您需要非常小心。有关处理此问题的方法,请参阅本文。

您需要将懒惰点匹配子模式放入第一个捕获组(第一组未加括号):

(?s)@@colored:(.*?) @color:(.*?)@@

请注意,为了使.与换行符匹配,您需要使用单行修饰符(内联(?s)RegexOptions.Singleline标志)。

并使用<p style='color:$2;'>$1</p>替换,其中$1表示some_text$2表示color

请参阅regex演示,这里有一个IDEONE演示:

var str = "some text @@colored:South Africa, officially the Republic of South Africa, is the southernmost country in Africa. It is bounded on the south by 2,798 kilometers of coastline of southern Africa stretching along the South Atlantic and Indian Oceans on the north by the neighbouring countries of Namibia, Botswana and Zimbabwe, and on the east by Mozambique and Swaziland, and surrounding the kingdom of Lesotho.[12] South Africa is the 25th-largest country in the world by land area, and with close to 53 million people, is the world's 24th-most populous nation. @color:red@@ another text";
Console.WriteLine(Regex.Replace(str, @"(?s)@@colored:(.*?) @color:(.*?)@@", "<p style='color:$2;'>$1</p>"));

我通常的警告:懒惰的点匹配可能会导致输入量很大的代码执行冻结。为了避免这种情况,请使用展开循环技术:

@@colored:([^ ]*(?: (?!@color:)[^ ]*)*) @color:([^@]*(?:@(?!@)[^@]*)*)@@

此正则表达式还有另一个优点:它不需要单行修饰符来匹配换行符。请参阅regex演示#2。