一直对Regex.Replace(string,string,string)这个没仔细了解
今天终于明白了
Regex.Replace(string input,string pattern,string replacement)
(1)
input: http://www.xxx.com/news/5/
pattern:.*?xxx\.com/news/\d+/
replacement:news.aspx
结果news.aspx
因为input按照pattern匹配的结果为http://www.xxx.com/news/5/,然后我们用news.aspx替换
最终结果就是news.aspx
(2)
input: abcedft http://www.xxx.com/news/5/6/7YYYYY
pattern: http://(.*/?)\.*xxx.com/news/(\d+/)*(\d+)/*
replacement: /news.aspx
结果为 abcedft/news.aspxYYYYY
因为input 中匹配的为 http://www.xxx.com/news/5/6/7
(3)
input:http://www.xxx.com/news/5/
pattern:http://(.*?)\.xxx.com/news/(\d+)/
replacement:news.aspx?id=$2
结果为:news.aspx?id=5
因为input按照pattern匹配的结果为http://www.xxx.com/news/5/,同时把(.*?)和(\d+)捕获到组里
然后用replacement来替换,$2代表的是获取第2个捕获组的值
零宽断言
(?<=exp)匹配exp后面的位置
(?=exp)匹配exp前面的位置
例如:
(?<=^|>)[^<>]+(?=<|$)
上面正则的意思就是匹配以[^<>]+这个开头或[^<>]+前面是>,同时[^<>]+后面是<或以[^<>]+结果的值
例子:
string test = "abc def <img src=\"124.jpg\"> ghi";
Regex reg = new Regex(@"(?<=^|>)[^<>]+(?=<|$)");
string result = reg.Replace(test, delegate(Match m) { return m.Value.Replace(" ", " ").Replace("\n", "<br>"); });
richTextBox2.Text = result;