Java中用户名的正则表达式(RegEx)

Java中用户名的正则表达式(RegEx),java,regex,Java,Regex,如何在Java中形成用户名字符串的正则表达式 练习规则: 只有3-10个字符 只允许使用'a'-'z','a'-'z','1'-'9','.'和'.' “u”和“.”只能出现0到2次 “abc.\uu”=false “abc…”=错误 “abc\uuu”=true “abc..”=true “abc”=true 如果我不使用正则表达式,它会更容易 在没有考虑“1”-“9”的情况下,我尝试了以下正则表达式,但它们不可行 String username_regex = "[a-zA-

如何在Java中形成用户名字符串的正则表达式

练习规则:

  • 只有3-10个字符
  • 只允许使用'a'-'z','a'-'z','1'-'9','.'和'.'
  • “u”和“.”只能出现0到2次
    • “abc.\uu”
      =false
    • “abc…”
      =错误
    • “abc\uuu”
      =true
    • “abc..”
      =true
    • “abc”
      =true
    如果我不使用正则表达式,它会更容易


    在没有考虑“1”-“9”的情况下,我尝试了以下正则表达式,但它们不可行

    String username_regex = "[a-zA-Z||[_||.]{0,2}]{3,10}";
    String username_regex = "[a-zA-Z]{3,10}||[_||.]{0,2}";
    
    我的职能:

    public static boolean isUserNameCorrect(String user_name) {
        String username_regex = "[a-zA-Z||[_]{0,2}]{3,10}";
        boolean isMatch = user_name.matches(username_regex);
        return isMatch;
    }
    
    我应该使用什么正则表达式?

    请尝试以下方法: [a-Z][0-9][.]?[[a-Z][0-9][.]?[[a-Z][0-9]*

    尼科

    编辑: 你说得对。然后是几个Regexp: Regex1:^[\w.]{3-10}$ Regex2:^[[a-Z][0-9]][[a-Z][0-9]][[a-Z][0-9]][[a-Z][0-9]]*$


    我希望我什么都没忘!

    可能不优雅,但你可以试试这个:

    ^(([A-Za-z0-9\._])(?!.*[\._].*[\._].*[\._])){3,10}$
    
    解释如下:

    NODE                     EXPLANATION
    --------------------------------------------------------------------------------
      ^                        the beginning of the string
    --------------------------------------------------------------------------------
      (                        group and capture to \1 (between 3 and 10
                               times (matching the most amount
                               possible)):
    --------------------------------------------------------------------------------
        (                        group and capture to \2:
    --------------------------------------------------------------------------------
          [A-Za-z0-9\._]           any character of: 'A' to 'Z', 'a' to
                                   'z', '0' to '9', '\.', '_'
    --------------------------------------------------------------------------------
        )                        end of \2
    --------------------------------------------------------------------------------
        (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
          .*                       any character except \n (0 or more
                                   times (matching the most amount
                                   possible))
    --------------------------------------------------------------------------------
          [\._]                    any character of: '\.', '_'
    --------------------------------------------------------------------------------
          .*                       any character except \n (0 or more
                                   times (matching the most amount
                                   possible))
    --------------------------------------------------------------------------------
          [\._]                    any character of: '\.', '_'
    --------------------------------------------------------------------------------
          .*                       any character except \n (0 or more
                                   times (matching the most amount
                                   possible))
    --------------------------------------------------------------------------------
          [\._]                    any character of: '\.', '_'
    --------------------------------------------------------------------------------
        )                        end of look-ahead
    --------------------------------------------------------------------------------
      ){3,10}                  end of \1 (NOTE: because you are using a
                               quantifier on this capture, only the LAST
                               repetition of the captured pattern will be
                               stored in \1)
    --------------------------------------------------------------------------------
      $                        before an optional \n, and the end of the
                               string
    

    这将满足您的上述要求。希望有帮助:)

    首先,
    |
    对于这个问题不是必需的,事实上,它并没有达到您认为的效果。我只见过它在regex的组中使用过(比如如果您想匹配
    Hello
    World
    ,您就要匹配
    (Hello | World)
    (?:Hello | World)
    ,在这些情况下,您只使用一个
    |


    接下来,让我解释一下为什么您尝试的每个正则表达式都不起作用

    String username_regex = "[a-zA-Z||[_||.]{0,2}]{3,10}";
    
    字符类中的范围运算符不会被解释为范围运算符,而是只表示组成范围运算符的文字。此外,嵌套字符类只是简单地组合在一起。因此,这实际上等于:

    String username_regex = "[a-zA-Z_|.{0,2}]{3,10}";
    
    因此,它将匹配以下3-10项的组合:
    a
    -
    z
    a
    -
    z
    0
    2
    {/code>,
    ,以及

    那不是你想要的


    这将匹配
    a
    -
    z
    a
    -
    z
    中的3到10个,然后是两个管道,然后是
    0到2次。这也不是您想要的


    实现这一点的简单方法是将需求分为两个部分,并基于这些部分创建两个正则表达式字符串:

  • 仅允许使用3-10个字符,其中仅允许使用'a'-'z'、'a'-'z'、'1'-'9'、'.'和'.'
  • “u”和“.”只能出现0到2次
  • 第一个要求非常简单:我们只需要创建一个包含所有有效字符的字符类,并限制这些字符的出现数量:

    "[a-zA-Z1-9_.]{3,10}"
    
    然后我将验证“u”和“.”是否出现0到2次:

    ".*[._].*[._].*"
    


    不幸的是,我没有足够的经验去弄清楚一个正则表达式是什么样子的……但是这些正则表达式至少是可读的。

    如果我在CS类中记忆犹新,那么创建一个正则表达式来满足所有三个要求是不可能的。因此,我会对每个条件进行单独检查。例如egex检查条件1和2,条件3单独检查

    private static final Pattern usernameRegex = Pattern.compile("[a-zA-Z1-9._]{3,10}");
    
    public static boolean isUserNameCorrect(String userName) {
        boolean isMatch = usernameRegex.matcher(userName).matches();
        return isMatch && countChar(userName, '.')<=2  && countChar(userName, '_') <=2;
    }
    
    public static int countChar(String s, char c) {
        int count = 0;
        int index = s.indexOf(c, 0);
        while ( index >= 0 ) {
            count++;
            index = s.indexOf(c, index+1);
        }
        return count;
    }
    
    private static final Pattern usernameRegex=Pattern.compile(“[a-zA-Z1-9.\uz1{3,10}”);
    公共静态布尔值isUserNameCorrect(字符串用户名){
    布尔isMatch=usernameRegex.matcher(userName.matches();
    
    返回isMatch&&countChar(用户名“.”)你在Regexone.com上解决练习吗
    |
    操作符的工作方式与他们在regexI中的工作方式不同。我会创建三个正则表达式:一个用于1,一个用于2,一个用于3。@sankrish-哦~这个练习不是来自Regexone.com,而是来自一个高级。这看起来好像会抛出一个
    模式语法表达式
    …更不用说了它似乎还允许用户名太长。非常感谢!但它显示了“无效的转义序列(有效的是\b\t\n\f\r\”\“\”\”)。如何解决它?将“\”更改为“\”?@CasperLi Yeap,您可以尝试将“\”替换为“\”
    "(?:.*[._].*){0,2}" // Might work, might not. Preferable to above regex if easy configuration is necessary. Might need reluctant quantifiers...
    
    private static final Pattern usernameRegex = Pattern.compile("[a-zA-Z1-9._]{3,10}");
    
    public static boolean isUserNameCorrect(String userName) {
        boolean isMatch = usernameRegex.matcher(userName).matches();
        return isMatch && countChar(userName, '.')<=2  && countChar(userName, '_') <=2;
    }
    
    public static int countChar(String s, char c) {
        int count = 0;
        int index = s.indexOf(c, 0);
        while ( index >= 0 ) {
            count++;
            index = s.indexOf(c, index+1);
        }
        return count;
    }