谈谈C# replace在正则表达式中的意义

2019-12-30 11:41:57刘景俊

转换ip地址


string t11 = "55.54.53.52"; 
  string p11 = "^" + 
   @"([01]?dd|2[0-4]d|25[0-5])." + 
   @"([01]?dd|2[0-4]d|25[0-5])." + 
   @"([01]?dd|2[0-4]d|25[0-5])." + 
   @"([01]?dd|2[0-4]d|25[0-5])" + 
   "$"; 
  Match m11 = Regex.Match(t11, p11); 

删除文件名包含的路径


string t12 = @"c:file.txt"; 
  string p12 = @"^.*"; 
  string r12 = Regex.Replace(t12, p12, ""); 

联接多行字符串中的行


string t13 = @"this is 
  a split line"; 
  string p13 = @"s*r?ns*"; 
  string r13 = Regex.Replace(t13, p13, " "); 

提取字符串中的所有数字


string t14 = @" 
  test 1 
  test 2.3 
  test 47 
  "; 
  string p14 = @"(d+.?d*|.d+)"; 
  MatchCollection mc14 = Regex.Matches(t14, p14); 

找出所有的大写字母


string t15 = "This IS a Test OF ALL Caps"; 
  string p15 = @"(b[^Wa-z0-9_]+b)"; 
  MatchCollection mc15 = Regex.Matches(t15, p15); 

找出小写的单词


string t16 = "This is A Test of lowercase"; 
  string p16 = @"(b[^WA-Z0-9_]+b)"; 
  MatchCollection mc16 = Regex.Matches(t16, p16); 

找出第一个字母为大写的单词


string t17 = "This is A Test of Initial Caps"; 
  string p17 = @"(b[^Wa-z0-9_][^WA-Z0-9_]*b)"; 
  MatchCollection mc17 = Regex.Matches(t17, p17); 

找出简单的HTML语言中的链接


string t18 = @" 
  <html> 
  <a href=""first.htm"">first tag text</a> 
  <a href=""next.htm"">next tag text</a> 
  </html> 
  "; 
  string p18 = @"<A[^>]*?HREFs*=s*[""']?" + @"([^'"" >]+?)[ '""]?>"; 
  MatchCollection mc18 = Regex.Matches(t18, p18, "si");

以上所述是小编给大家分享的C# replace在正则表达式中的意义,希望对大家有所帮助!



注:相关教程知识阅读请移步到c#教程频道。