【Go】常用的正则表达式

来源:互联网 发布:微云同步盘 mac 编辑:程序博客网 时间:2024/06/06 10:49
/************************************************************名字 golang 正则工具*功能 支持数字,字母,字符,常用信息(电话,邮箱)等的正则匹配*作者 Razil************************************************************/package modelsimport ("regexp")type RegexCheck struct {}/************************* 自定义类型 ************************///数字+字母  不限制大小写 6~30位func (ic *RegexCheck) IsID(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^[0-9a-zA-Z]{6,30}$", s)if false == b {return b}}return b}//数字+字母+符号 6~30位func (ic *RegexCheck) IsPwd(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^[0-9a-zA-Z@.]{6,30}$", s)if false == b {return b}}return b}/************************* 数字类型 ************************///纯整数func (ic *RegexCheck) IsInteger(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^[0-9]+$", s)if false == b {return b}}return b}//纯小数func (ic *RegexCheck) IsDecimals(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^\\d+\\.[0-9]+$", s)if false == b {return b}}return b}//手提电话(不带前缀)最高11位func (ic *RegexCheck) IsCellphone(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^1[0-9]{10}$", s)if false == b {return b}}return b}//家用电话(不带前缀) 最高8位func (ic *RegexCheck) IsTelephone(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^[0-9]{8}$", s)if false == b {return b}}return b}/************************* 英文类型 *************************///仅小写func (ic *RegexCheck) IsEngishLowCase(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^[a-z]+$", s)if false == b {return b}}return b}//仅大写func (ic *RegexCheck) IsEnglishCap(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^[A-Z]+$", s)if false == b {return b}}return b}//大小写混合func (ic *RegexCheck) IsEnglish(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^[A-Za-z]+$", s)if false == b {return b}}return b}//邮箱 最高30位func (ic *RegexCheck) IsEmail(str ...string) bool {var b boolfor _, s := range str {b, _ = regexp.MatchString("^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$", s)if false == b {return b}}return b}

 

使用方法。复制保存为.go文件,即可使用。使用前应修改package为所在文件夹名。