|
引用命名空间using System.Text.RegularExpressions;
/* 在名字空间中仅仅包含着6个类和一个定义,它们是:
Capture: 包含一次匹配的结果; CaptureCollection: Capture的序列; Group: 一次组记录的结果,由Capture继承而来; Match: 一次表达式的匹配结果,由Group继承而来; MatchCollection: Match的一个序列; MatchEvaluator: 执行替换操作时使用的代理; Regex: 编译后的表达式的实例。 Regex类中还包含一些静态的方法: Escape: 对字符串中的regex中的转义符进行转义; IsMatch: 如果表达式在字符串中匹配,该方法返回一个布尔值; Match: 返回Match的实例; Matches: 返回一系列的Match的方法; Replace: 用替换字符串替换匹配的表达式; Split: 返回一系列由表达式决定的字符串; Unescape:不对字符串中的转义字符转义。 */
1.一般性处理:
[csharp]
view plain
copy
print
?
- Regex r = new Regex("abc");
- Match m = r.Match("123abcff");
- if (m.Success)
- Response.Write("found match at postion " + m.Index.ToString() + "," + m.ToString() + "<br />");
Regex r = new Regex("abc");
Match m = r.Match("123abcff");
if (m.Success)
Response.Write("found match at postion " + m.Index.ToString() + "," + m.ToString() + "<br />");
Success方法,表示该匹配是否成功,成功了就输出找到它的位置(索引是从0开始的);
2.MatchCollection,它表示通过以迭代方式将正则表达式模式应用于输入字符串所找到的成功匹配的集合:
[csharp]
view plain
copy
print
?
- MatchCollection mc = r.Matches("123abc4abcd"); //此处是Matches
MatchCollection mc = r.Matches("123abc4abcd"); //此处是Matches
输出来看看:
string[] results = new string[20]; int[] matchposition = new int[20];
[csharp]
view plain
copy
print
?
- for (int i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
- {
- results[i] = mc[i].Value; //将匹配的字符串添在字符串数组中
- matchposition[i] = mc[i].Index; //记录匹配字符的位置
- }
for (int i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
{
results[i] = mc[i].Value; //将匹配的字符串添在字符串数组中
matchposition[i] = mc[i].Index; //记录匹配字符的位置
}当然,如果我们用linq来处理视乎就更简单了
[csharp]
view plain
copy
print
?
- string[] strArr = r.Matches("123abc4abcd").Cast<Match>().Select(t => t.Value + ":" + t.Index.ToString()).ToArray();
- Response.Write("<strong>对123abc4abcd进行abc匹配,结果:</strong>");
- foreach (string str in strArr)
- Response.Write(str + ",");
string[] strArr = r.Matches("123abc4abcd").Cast<Match>().Select(t => t.Value + ":" + t.Index.ToString()).ToArray();
Response.Write("<strong>对123abc4abcd进行abc匹配,结果:</strong>");
foreach (string str in strArr)
Response.Write(str + ",");
3.关于replace,它有3个重载,举个例子
[csharp]
view plain
copy
print
?
- static string CapText(Match m)
- {
- string x = m.ToString();//取得匹配的字符串
- if (char.IsLower(x[0]))// 如果第一个字符是小写就转换为大写
- return char.ToUpper(x[0]) + x.Substring(1,x.Length - 1);
-
- return x;
- }
static string CapText(Match m)
{
string x = m.ToString();//取得匹配的字符串
if (char.IsLower(x[0]))// 如果第一个字符是小写就转换为大写
return char.ToUpper(x[0]) + x.Substring(1,x.Length - 1);
return x;
}
[csharp]
view plain
copy
print
?
- string text = @"One is always on a strange road,watching strange scenery and listening to strange music.
- Then one day,you will find that the things you try hard to forget are already gone.";
- Response.Write("<strong>result:</strong>" + text + "<br />");
- string result = Regex.Replace(text,@"w+",new MatchEvaluator(CapText));
- Response.Write("<strong>result changed:</strong>" + result);
string text = @"One is always on a strange road,watching strange scenery and listening to strange music.
Then one day,you will find that the things you try hard to forget are already gone.";
Response.Write("<strong>result:</strong>" + text + "<br />");
string result = Regex.Replace(text,@"w+",new MatchEvaluator(CapText));
Response.Write("<strong>result changed:</strong>" + result);
4.关于MatchEvaluator,
[csharp]
view plain
copy
print
?
- public delegate string MatchEvaluator(Match match);
public delegate string MatchEvaluator(Match match); 其中参数match是System.Text.RegularExpressions.Match对象,作方法操作过程中的单个正则表达式匹配。下面举例:
先了解下正则表达式的字符解释:
好的,我们来测试下:
[csharp]
view plain
copy
print
?
- string str = "<You're angle & evil>";
- Regex regex = new Regex("'|&|<|>");
- MatchEvaluator evaluator = new MatchEvaluator(ConvertToXML);
- string result = regex.Replace(str,evaluator); //<You&aposre angle & evil>
string str = "<You're angle & evil>";
Regex regex = new Regex("'|&|<|>");
MatchEvaluator evaluator = new MatchEvaluator(ConvertToXML);
string result = regex.Replace(str,evaluator); //<You&aposre angle & evil>
[csharp]
view plain
copy
print
?
- public string ConvertToXML(Match m)
- {
- switch (m.Value)
- {
- case "'":
- return "&apos";
- case "&":
- return "&";
- case "<":
- return "<";
- case ">":
- return ">";
- default:
- return "";
- }
- }
public string ConvertToXML(Match m)
{
switch (m.Value)
{
case "'":
return "&apos";
case "&":
return "&";
case "<":
return "<";
case ">":
return ">";
default:
return "";
}
}
下面附上正则表达式的全部符号解释:
(编辑:安卓应用网)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|