自己写的类,方便自己使用的。使用正则表达式匹配阿,找到匹配的项阿,替换阿。不对的,请高手斧正阿。
使用的方法是:
要添加一些自己常用的正则表达式的话:
注意:
委托 RegexpType中的
onlyOneToNine 关联的是 regexStrs [0];
name 是要和 regexStrs 相关联。
一般要就在最加就可以了。
using
System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
namespace zdHelper
{
static public class zdRegexpHelper
{
/// <summary>
/// 是否符合指定的正则表达式
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public bool Validate(string str, RegexpType type)
{
Regex regex = new Regex(regexStrs[(int)type].ToString());
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
///是否符合指定的正则表达式
static public bool Validate(string str, string regexStr)
{
Regex regex = new Regex(regexStr);
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
/// <summary>
/// 是否全部符合指定的正则表达式
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public bool AllValidate(string str, RegexpType type)
{
Regex regex = new Regex("^" + regexStrs[(int)type].ToString() + "$");
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
/// <summary>
/// 是否全部符合指定的正则表达式
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public bool AllValidate(string str, string regexStr)
{
Regex regex = new Regex(regexStr);
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
/// <summary>
/// 返回符合的部分
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public ArrayList GetValidatedStr(string str, RegexpType type)
{
ArrayList array = new ArrayList();
Regex regex = new Regex(regexStrs[(int)type].ToString());
Match match = regex.Match(str);
while (match.Success)
{
array.Add(match.Value.ToString());
match= match.NextMatch();
}
return array;
}
/// <summary>
/// 返回符合的部分
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public ArrayList GetValidatedStr(string str, string regexStr)
{
ArrayList array = new ArrayList();
Regex regex = new Regex(regexStr.ToString());
Match match = regex.Match(str);
while (match.Success)
{
array.Add(match.Value.ToString());
match = match.NextMatch();
}
return array;
}
///用replaceStr代替str中指定的正则表达式匹配的部分
static public string ReplaceStrUseRegex(string str, RegexpType type,string replaceStr)
{
string replacedStr=Regex.Replace(str, regexStrs[(int)type].ToString(), replaceStr);
return replacedStr;
}
///用replaceStr代替str中指定的正则表达式匹配的部分
static public string ReplaceStrUseRegex(string str, string regexStr,string replaceStr)
{
string replacedStr=Regex.Replace(str, regexStr.ToString(), replaceStr);
return replacedStr;
}
static string[] regexStrs = new string[] { "[0-9]*”, "/d{1,}", "/+?[1-9][0-9]*", "/-[1-9][0-9]*", "[A-Za-z]+", "[A-Za-z0-9]+", "/w+", "[a-zA-Z]/w{5,17}", "[^%&',;=?$"]+", "[//u4e00-//u9fa5]{1,}","/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*","http://([/w-]+/.)+[/w-]+(/[/w-./?%&=]*)?","(/(/d{3,4}-)|/d{3.4}-)?/d{7,8}" ,"[1-9]([0-9]{16}|[0-9]{13})[xX0-9]"};
}
///默认的一些正则表达式类型
public enum RegexpType
{
onlyOneToNine,//仅从1到9
num,//数字
positiveInt,//正整数
minusInt,//副整数
letter,//字母
intOrLetter,//整数或字母
intOrLetterOrUnderline,//整数或字母或下划线
possword,//密码,整数或字母或下划线,字母开头,6到18位
especialSign,//特殊符号
Chinese,//汉字
Email,//不用说啦
InternetURL,//网址
phone,//也不用说啦
identityCard,//身份证
}
}
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
namespace zdHelper
{
static public class zdRegexpHelper
{
/// <summary>
/// 是否符合指定的正则表达式
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public bool Validate(string str, RegexpType type)
{
Regex regex = new Regex(regexStrs[(int)type].ToString());
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
///是否符合指定的正则表达式
static public bool Validate(string str, string regexStr)
{
Regex regex = new Regex(regexStr);
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
/// <summary>
/// 是否全部符合指定的正则表达式
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public bool AllValidate(string str, RegexpType type)
{
Regex regex = new Regex("^" + regexStrs[(int)type].ToString() + "$");
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
/// <summary>
/// 是否全部符合指定的正则表达式
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public bool AllValidate(string str, string regexStr)
{
Regex regex = new Regex(regexStr);
Match match = regex.Match(str);
if (match.Success)
return true;
else
return false;
}
/// <summary>
/// 返回符合的部分
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public ArrayList GetValidatedStr(string str, RegexpType type)
{
ArrayList array = new ArrayList();
Regex regex = new Regex(regexStrs[(int)type].ToString());
Match match = regex.Match(str);
while (match.Success)
{
array.Add(match.Value.ToString());
match= match.NextMatch();
}
return array;
}
/// <summary>
/// 返回符合的部分
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
static public ArrayList GetValidatedStr(string str, string regexStr)
{
ArrayList array = new ArrayList();
Regex regex = new Regex(regexStr.ToString());
Match match = regex.Match(str);
while (match.Success)
{
array.Add(match.Value.ToString());
match = match.NextMatch();
}
return array;
}
///用replaceStr代替str中指定的正则表达式匹配的部分
static public string ReplaceStrUseRegex(string str, RegexpType type,string replaceStr)
{
string replacedStr=Regex.Replace(str, regexStrs[(int)type].ToString(), replaceStr);
return replacedStr;
}
///用replaceStr代替str中指定的正则表达式匹配的部分
static public string ReplaceStrUseRegex(string str, string regexStr,string replaceStr)
{
string replacedStr=Regex.Replace(str, regexStr.ToString(), replaceStr);
return replacedStr;
}
static string[] regexStrs = new string[] { "[0-9]*”, "/d{1,}", "/+?[1-9][0-9]*", "/-[1-9][0-9]*", "[A-Za-z]+", "[A-Za-z0-9]+", "/w+", "[a-zA-Z]/w{5,17}", "[^%&',;=?$"]+", "[//u4e00-//u9fa5]{1,}","/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*","http://([/w-]+/.)+[/w-]+(/[/w-./?%&=]*)?","(/(/d{3,4}-)|/d{3.4}-)?/d{7,8}" ,"[1-9]([0-9]{16}|[0-9]{13})[xX0-9]"};
}
///默认的一些正则表达式类型
public enum RegexpType
{
onlyOneToNine,//仅从1到9
num,//数字
positiveInt,//正整数
minusInt,//副整数
letter,//字母
intOrLetter,//整数或字母
intOrLetterOrUnderline,//整数或字母或下划线
possword,//密码,整数或字母或下划线,字母开头,6到18位
especialSign,//特殊符号
Chinese,//汉字
Email,//不用说啦
InternetURL,//网址
phone,//也不用说啦
identityCard,//身份证
}
}
string
str
=
"
1238974
"
;
bool a = zdRegexpHelper.Validate(str, RegexpType.num);
ArrayList b = zdRegexpHelper.GetValidatedStr(str, RegexpType.num);
// 或者
bool a = zdRegexpHelper.Validate(str, “[ 0 - 9 ] * “);
ArrayList b = zdRegexpHelper.GetValidatedStr(str, “[ 0 - 9 ] * “);
bool a = zdRegexpHelper.Validate(str, RegexpType.num);
ArrayList b = zdRegexpHelper.GetValidatedStr(str, RegexpType.num);
// 或者
bool a = zdRegexpHelper.Validate(str, “[ 0 - 9 ] * “);
ArrayList b = zdRegexpHelper.GetValidatedStr(str, “[ 0 - 9 ] * “);
static
string
[] regexStrs
=
new
string
[]
{ "[0-9]*", "/d{1,}", "/+?[1-9][0-9]*", "/-[1-9][0-9]*", "[A-Za-z]+", "[A-Za-z0-9]+", "/w+", "[a-zA-Z]/w{5,17}", "[^%&',;=?$"]+", "[/u4e00-/u9fa5]{1,}","/w+([-+.]/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*","http://([/w-]+/.)+[/w-]+(/[/w-./?%&=]*)?","(/(/d{3,4}-)|/d{3.4}-)?/d{7,8}" ,"[1-9]([0-9]{16}|[0-9]{13})[xX0-9]",str}
;
//
str是你需要的正则表达式
public enum RegexpType
{
onlyOneToNine,
num,
positiveInt,
minusInt,
letter,
intOrLetter,
intOrLetterOrUnderline,
possword,
especialSign,
Chinese,
Email,
InternetURL,
phone,
identityCard,
name,//是你给的名字
}
public enum RegexpType
{
onlyOneToNine,
num,
positiveInt,
minusInt,
letter,
intOrLetter,
intOrLetterOrUnderline,
possword,
especialSign,
Chinese,
Email,
InternetURL,
phone,
identityCard,
name,//是你给的名字
}
委托 RegexpType中的
onlyOneToNine 关联的是 regexStrs [0];
name 是要和 regexStrs 相关联。
一般要就在最加就可以了。