命名空间: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);
}
}
}
}