github.com/webx-top/com@v1.2.12/regex.go (about)

     1  // Copyright 2013 com authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"): you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package com
    16  
    17  import "regexp"
    18  
    19  const (
    20  	regexEmailPattern       = `(?i)[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}`
    21  	regexStrictEmailPattern = `(?i)[A-Z0-9!#$%&'*+/=?^_{|}~-]+` +
    22  		`(?:\.[A-Z0-9!#$%&'*+/=?^_{|}~-]+)*` +
    23  		`@(?:[A-Z0-9](?:[A-Z0-9-]*[A-Z0-9])?\.)+` +
    24  		`[A-Z0-9](?:[A-Z0-9-]*[A-Z0-9])?`
    25  	regexURLPattern                          = `(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?`
    26  	regexUsernamePattern                     = `^[\w\p{Han}]+$`
    27  	regexChinesePattern                      = `^[\p{Han}]+$`
    28  	regexChineseFirstPattern                 = `^[\p{Han}]`
    29  	regexContainsChinesePattern              = `[\p{Han}]+`
    30  	regexEOLPattern                          = "[\r\n]+"
    31  	regexAlphaNumericUnderscorePattern       = `^[a-zA-Z0-9_]+$`
    32  	regexAlphaNumericUnderscoreHyphenPattern = `^[a-zA-Z0-9_-]+$`
    33  )
    34  
    35  var (
    36  	regexEmail                        = regexp.MustCompile(regexEmailPattern)
    37  	regexStrictEmail                  = regexp.MustCompile(regexStrictEmailPattern)
    38  	regexURL                          = regexp.MustCompile(regexURLPattern)
    39  	regexUsername                     = regexp.MustCompile(regexUsernamePattern)
    40  	regexChinese                      = regexp.MustCompile(regexChinesePattern)
    41  	regexContainsChinese              = regexp.MustCompile(regexContainsChinesePattern)
    42  	regexChinesePrefix                = regexp.MustCompile(regexChineseFirstPattern)
    43  	regexEOL                          = regexp.MustCompile(regexEOLPattern)
    44  	regexAlphaNumericUnderscore       = regexp.MustCompile(regexAlphaNumericUnderscorePattern)
    45  	regexAlphaNumericUnderscoreHyphen = regexp.MustCompile(regexAlphaNumericUnderscoreHyphenPattern)
    46  	regexFloat                        = regexp.MustCompile(`^[-]?[\d]+\.[\d]+$`)
    47  	regexInteger                      = regexp.MustCompile(`^[-]?[\d]+$`)
    48  	regexUnsignedInteger              = regexp.MustCompile(`^[\d]+$`)
    49  )
    50  
    51  // IsAlphaNumericUnderscore 是否仅仅包含字母、数字、和下划线
    52  func IsAlphaNumericUnderscore(s string) bool {
    53  	return regexAlphaNumericUnderscore.MatchString(s)
    54  }
    55  
    56  // IsAlphaNumericUnderscoreHyphen 是否仅仅包含字母、数字、下划线和连字符(-)
    57  func IsAlphaNumericUnderscoreHyphen(s string) bool {
    58  	return regexAlphaNumericUnderscoreHyphen.MatchString(s)
    59  }
    60  
    61  // IsEmail validate string is an email address, if not return false
    62  // basically validation can match 99% cases
    63  func IsEmail(email string) bool {
    64  	return regexEmail.MatchString(email)
    65  }
    66  
    67  // IsEmailRFC validate string is an email address, if not return false
    68  // this validation omits RFC 2822
    69  func IsEmailRFC(email string) bool {
    70  	return regexStrictEmail.MatchString(email)
    71  }
    72  
    73  // IsURL validate string is a url link, if not return false
    74  // simple validation can match 99% cases
    75  func IsURL(url string) bool {
    76  	return regexURL.MatchString(url)
    77  }
    78  
    79  // IsUsername validate string is a available username
    80  func IsUsername(username string) bool {
    81  	return regexUsername.MatchString(username)
    82  }
    83  
    84  // IsChinese validate string is Chinese
    85  func IsChinese(str string) bool {
    86  	return regexChinese.MatchString(str)
    87  }
    88  
    89  // HasChinese contains Chinese
    90  func HasChinese(str string) bool {
    91  	return regexContainsChinese.MatchString(str)
    92  }
    93  
    94  func HasChineseFirst(str string) bool {
    95  	return regexChinesePrefix.MatchString(str)
    96  }
    97  
    98  // IsSingleLineText validate string is a single-line text
    99  func IsSingleLineText(text string) bool {
   100  	return !regexEOL.MatchString(text)
   101  }
   102  
   103  // IsMultiLineText validate string is a multi-line text
   104  func IsMultiLineText(text string) bool {
   105  	return regexEOL.MatchString(text)
   106  }
   107  
   108  // IsFloat validate string is a float number
   109  func IsFloat(val string) bool {
   110  	return regexFloat.MatchString(val)
   111  }
   112  
   113  // IsInteger validate string is a integer
   114  func IsInteger(val string) bool {
   115  	return regexInteger.MatchString(val)
   116  }
   117  
   118  // IsUnsignedInteger validate string is a unsigned-integer
   119  func IsUnsignedInteger(val string) bool {
   120  	return regexUnsignedInteger.MatchString(val)
   121  }
   122  
   123  // RemoveEOL remove \r and \n
   124  func RemoveEOL(text string) string {
   125  	return regexEOL.ReplaceAllString(text, ` `)
   126  }
   127  
   128  // FindChineseWords find chinese words
   129  func FindChineseWords(text string, n ...int) []string {
   130  	var _n int
   131  	if len(n) > 0 {
   132  		_n = n[0]
   133  	} else {
   134  		_n = -1
   135  	}
   136  	matches := regexContainsChinese.FindAllStringSubmatch(text, _n)
   137  	var result []string
   138  	for _, words := range matches {
   139  		result = append(result, words...)
   140  	}
   141  	return result
   142  }