对于正则表达式的几具尝试
static void Main(string[] args)
{
string input = "This is,text with,far%too much whitespace.";
string pattern = ",";
string replacement = "&";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);
Console.WriteLine("Original String: {0}", input);
Console.WriteLine("Replacement String: {0}", result);
Console.Read();
}
输出结果:
Original String : This is,text with,far%too much whitespace.
Replacement String: This is&text with&far%too much whitespace.
2).
string pattern = ",|%";
输出结果:
Original String : This is,text with,far%too much whitespace.
Replacement String: This is&text with&far&too much whitespace.
3).
string pattern = "[,%]";
输出结果:
Original String : This is,text with,far%too much whitespace.
Replacement String: This is&text with&far&too much whitespace.
4).
string pattern = ",%";
Original String : This is,text with,far%too much whitespace.
Replacement String: This is,text with,far%too much whitespace.
5)
string input = "This is,text with,far%too much,% whitespace.";
string pattern = "(,%)";
输出结果:
Original String : This is,text with,far%too much,% whitespace.
Replacement String: This is,text with,far%too much& whitespace.
总结下:
[ ]:选择, ():整体, |:或