refer to:http://www.codeproject.com/Articles/368258/I-dont-like-Regex
字符串扩展方法类:
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace RegexExtract
{
static class StringExtract
{
public static bool Like(this string item, string searchPattern)
{
return GetRegex("^" + searchPattern).IsMatch(item);
}
public static string Search(this string item, string searchPattern)
{
var match = GetRegex(searchPattern).Match(item);
if (match.Success)
{
return item.Substring(match.Index, match.Length);
}
return null;
}
public static List<string> Extract(this string item, string searchPattern)
{
var result = item.Search(searchPattern);
if (!string.IsNullOrWhiteSpace(result))
{
var splitted = searchPattern.Split(new[] { '?', '%', '*', '#' }, StringSplitOptions.RemoveEmptyEntries);
var temp = result;
var final = new List<string>();
Array.ForEach(splitted, x =>
{
var pos = temp.IndexOf(x, StringComparison.Ordinal);
if (pos > 0)
{
final.Add(temp.Substring(0, pos));
temp = temp.Substring(pos);
}
temp = temp.Substring(x.Length);
});
if (temp.Length > 0) final.Add(temp);
return final;
}
return null;
}
// private method which accepts the simplified pattern and transform it into a valid .net regex pattern:
// it escapes standard regex syntax reserved characters
// and transforms the simplified syntax into the native Regex one
static Regex GetRegex(string searchPattern)
{
return new Regex(searchPattern
.Replace("\\", "\\\\")
.Replace(".", "\\.")
.Replace("{", "\\{")
.Replace("}", "\\}")
.Replace("[", "\\[")
.Replace("]", "\\]")
.Replace("+", "\\+")
.Replace("$", "\\$")
.Replace(" ", "\\s")
.Replace("#", "[0-9]")
.Replace("?", ".")
.Replace("*", "\\w*")
.Replace("%", ".*")
, RegexOptions.IgnoreCase);
}
}
}
测试用例:
using System;
namespace RegexExtract
{
class Program
{
static void Main()
{
//example: a string is a guid
var result = "TA0E02391-A0DF-4772-B39A-C11F7D63C495".Like("?????????-????-????-????-????????????");
Console.WriteLine(result);
//example: a string starts with a guid
result = "This is a guid TA0E02391-A0DF-4772-B39A-C11F7D63C495".Like("%?????????-????-????-????-????????????");
Console.WriteLine(result);
//example: a string ends with a guid
result = "TA0E02391-A0DF-4772-B39A-C11F7D63C495 is a guid".Like("?????????-????-????-????-????????????%");
Console.WriteLine(result);
//example: a string contains a guid
result = "this string TA0E02391-A0DF-4772-B39A-C11F7D63C495 contains a guid".Like("%?????????-????-????-????-????????????%");
Console.WriteLine(result);
//example: Search for a guid inside a text
var strResult =
"this string [TA0E02391-A0DF-4772-B39A-C11F7D63C495] contains a string matching".Search(
"[?????????-????-????-????-????????????]");
Console.WriteLine(strResult); // output: [TA0E02391-A0DF-4772-B39A-C11F7D63C495]
//example: retrieving the consituents of a guid inside a text
var listResult =
"this string [TA0E02391-A0DF-4772-B39A-C11F7D63C495] contains a string matching".Extract(
"[?????????-????-????-????-????????????]");
foreach (var str in listResult)
{
Console.WriteLine(str);
}
// result is an array containing each part of the pattern: {"TA0E02391", "A0DF", "4772", "B39A", "C11F7D63C495"}
//example: retrieving the consituents of an email inside a text
listResult = "this string contains an email: toto@domain.com".Extract("*?@?*.?*");
foreach (var str in listResult)
{
Console.WriteLine(str);
}
// result is an array containing each part of the pattern: {"toto", "domain", "com"}
Console.ReadKey();
}
}
}