用于替换标签的正则表达式

时间:2015-12-02 10:01:06

标签: c# regex

我需要像这样替换字符串

@@colored:some_text @color:clr@@

使用以下html标记:

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

我写了一个正则表达式来搜索这样的文本片段,但我不知道怎么做替换。 Here是我的正则表达式的一个例子

以下是我尝试执行此操作的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();
    }

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

2 个答案:

答案 0 :(得分:1)

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

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

$2指的是(.*?)捕获组的内容; $1是指((?:.|\n)*?)的内容。请注意使用非捕获括号(?: ...)进行分组而不创建捕获组。但是,由于回溯,这可能导致显着的减速,因此您需要非常小心。有关解决问题的方法,请参阅this article

答案 1 :(得分:1)

您需要将延迟点匹配子模式放入第一个捕获组(第一组非转义括号):

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

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

并使用<p style='color:$2;'>$1</p>替换$1引用some_text$2引用color

请参阅regex demo,此处为IDEONE demo

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

我通常的警告:懒惰点匹配可能会导致代码执行因非常大的输入而冻结。要避免它,请使用unroll-the-loop技术:

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

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