zoukankan      html  css  js  c++  java
  • LUA检查邮箱地址是否合法

    郑重声明:本文是笔者根据个人理解所写,错误难免,欢迎拍砖!

           可以任意转载、修改,转载时是否标明出处,随君而定!

    合法的email地址格式如下:
      1. 首字符必须用字母,而且其它的字符只能用26个大小写字母、0~9及_-.@符号
      2. 必须包含一个并且只有一个符号“@”
      3. @后必须包含至少一个至多三个符号“.”
      4. 第一个字符不得是“@”或者“.”(第一步已检查过了)
      5. 不允许出现“@.”或者.@
      6. 结尾不得是字符“@”或者“.”

    具体代码实现:

     1 function isRightEmail(str)
     2     if string.len(str or "") < 6 then return false end
     3     local b,e = string.find(str or "", '@')
     4     local bstr = ""
     5     local estr = ""
     6     if b then
     7         bstr = string.sub(str, 1, b-1)
     8         estr = string.sub(str, e+1, -1)
     9     else
    10         return false
    11     end
    12 
    13     -- check the string before '@'
    14     local p1,p2 = string.find(bstr, "[%w_]+")
    15     if (p1 ~= 1) or (p2 ~= string.len(bstr)) then return false end
    16     
    17     -- check the string after '@'
    18     if string.find(estr, "^[%.]+") then return false end
    19     if string.find(estr, "%.[%.]+") then return false end
    20     if string.find(estr, "@") then return false end
    21     if string.find(estr, "[%.]+$") then return false end
    22 
    23     _,count = string.gsub(estr, "%.", "")
    24     if (count < 1 ) or (count > 3) then
    25         return false
    26     end
    27 
    28     return true
    29 end
  • 相关阅读:
    LeetCode-Palindrome Partitioning II
    LeetCode-Palindrome Partitioning
    LeetCode-Permutation Sequence
    LeetCode-Anagrams
    LeetCode-Text Justification
    LeetCode-Best Time to Buy and Sell Stock III
    LeetCode-Best Time to Buy and Sell Stock II
    LeetCode-Best Time to Buy and Sell Stock
    LeetCode-N-Queens II
    BZOJ 5390: [Lydsy1806月赛]糖果商店
  • 原文地址:https://www.cnblogs.com/520zijuan/p/2892929.html
Copyright © 2011-2022 走看看