c#正则表达式

命名空间:System.Text.RegularExpressions
­
替换多个字符:
Regex.Replace(tm, @"[///:*?""<>|]", "");
判断字符串是否是数字
Regex.IsMatch("23ewrwef323", @"^/d+$");
将字符串拆分多个关键字的数组
Regex.Split(s_tr, "[-]+")
判断多个关键字
Regex.IsMatch(str,@"(?i:asd|kk|due)",System.Text.RegularExpressions.RegexOptions.IgnoreCase)
判断多个文件扩展名
Regex.IsMatch(str,@"/.(?i:jpg|bmp|tiff)",System.Text.RegularExpressions.RegexOptions.IgnoreCase)
只保留中文
Regex.Replace("abcd1234只保留中文apbjk23保留中文", @"[^/u4e00-/u9fa5]+", "");
只保留英文
Regex.Replace("abcd1QWEQWE只保留英文apWERjk23保留英文", @"[^A-Za-z]+", "");
只保留数字
Regex.Replace("ab4897QW45只保留数字apW1234jk23保留数字", @"[^0-9]+", "");
多个空格替换成一个空格
Regex.Replace("替换空格    12312412   werwer", "[//s]+", " ");
­
C#例子:
­
using System;
using System.Text.RegularExpressions;
public class Test
{    
      public static void Main ()    
     {          
           // Define a regular expression for currency values.          
           Regex rx = new Regex(@"^-?/d+(/./d{2})?$");  
­
          // Define some test strings.          
          string[] tests = {"-42", "19.99", "0.001", "100 USD"};                    
­
         // Check each test string against the regular expression.          
         foreach (string test in tests)          
        {            
                  if (rx.IsMatch(test))              
                  {                  
                         Console.WriteLine("{0} is a currency value.", test);              
                  }
                 else              
                {                  
                        Console.WriteLine("{0} is not a currency value.", test);              
                }        
         }            
     }        
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值