C#中的正则表达式(正则表达式)

时间:2011-03-31 11:29:30

标签: c# regex

我怎样才能在C#中替换字符串

I need //cursive// text and //cursive// text.

INTO:

I need <i>cursive</i> text and <i>cursive</i> text.

This si me [website[http://www.website.com]] and other me [website[http://www.website2.com]].

INTO:

This si me <a href='http://www.website.com'>website</a> and other me <a href='http://www.website2.com'>website</a>.

第一幕的解决方案:

string Test = "Mama ma //hodne// prace protoze //neni// doma.";
string Result = Regex.Replace(Test, "//(.+?)//", "<i>$1</i>");

第二幕的解决方案:

string Test2 = "Mama ma [hodne[http://www.someurl.com]]  protoze [neni[http://www.someurl.com]] doma.";
string Result2 = Regex.Replace(Test2, "\\[(.+?)\\[(.+?)\\]\\]", "<a href='$2' target='_blank'>$1</a>");

1 个答案:

答案 0 :(得分:0)

JinDa发现的解决方案:

第一幕的解决方案:

string Test = "Mama ma //hodne// prace protoze //neni// doma.";
string Result = Regex.Replace(Test, "//(.+?)//", "<i>$1</i>");

第二幕的解决方案:

string Test2 = "Mama ma [hodne[http://www.someurl.com]]  protoze [neni[http://www.someurl.com]] doma.";
string Result2 = Regex.Replace(Test2, "\\[(.+?)\\[(.+?)\\]\\]", "<a href='$2' target='_blank'>$1</a>");