2014-05-23 16 views
1

如何在Java中构成用户名字符串的RegEx?Java中用户名的正则表达式(RegEx)

规则练习:

  1. 只有3 - 10个字符。
  2. 只有'a' - 'z','A' - 'Z','1' - '9','_'和'。'被允许。
  3. '_'和'。'只能出现0到2次。
    • "abc_._" =假
    • "abc..." =假
    • "abc__" =真
    • "abc.." =真
    • "abc_." =真

如果我不使用正则表达式,这将是更轻松。


不考虑'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; 
} 

什么的正则表达式应该怎么使用?

谢谢您的关注。

+0

你解决从Regexone.com –

+2

演习''||操作不起作用他们认为他们在正则表达式 – awksp

+3

做的方式我会创建三个正则表达式:一为#1,一个用于#2和一个为#3。 – sp00m

回答

0

请试试这个:?? [AZ] [0-9] [._] [AZ] [0-9] [._] [AZ] [0-9] *

尼科

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

我希望我什么也没忘!

+0

这看起来好像会抛出一个'PatternSyntaxExpression' ......更不用说它似乎也会允许用户名太长。 – awksp

0

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

^(([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 

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

+0

这给编译错误... – awksp

+0

非常感谢你!但它显示我“无效的转义序列(有效的转义序列是\ b \ t \ n \ f \ r \”\'\\)“。如何解决它?将'\'更改为”\\“? – Casper

+0

@CasperLi Yeap ,您可以通过将“\”替换为“\\” – Dale

1

首先,||不是这个问题的必要条件,事实上并没有做你认为它的工作。我只见过它用于正则表达式的组(如果你想匹配HelloWorld,你会匹配(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 - zA - Z02{}.|_

而这不是你想要的。


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

这将匹配3的a 10 - zA - Z,其次是两个管道,随后_|,或. 0〜2倍。也不是你想要的。


最简单的方式做,这是通过拆分需求分为两个部分,并创建基于送行的两个正则表达式的字符串:

  1. 只有3 - 10个字符,其中只有“A'-” z','A' - 'Z','1' - '9','_'和'。'被允许。
  2. '_'和'。'只能出现0到2次。

的第一个要求很简单:我们只需要创建一个字符类包括有多少能出现在所有有效字符和地点的限制:

"[a-zA-Z1-9_.]{3,10}" 

然后我会验证“_ '和'。'出现0至2次:

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

"(?:.*[._].*){0,2}" // Might work, might not. Preferable to above regex if easy configuration is necessary. Might need reluctant quantifiers... 

我遗憾的是没有足够的经验来找出一个单一的正则表达式是什么样子......但这些都至少相当的可读性。

2

如果我记得CS类很好,它是而不是可能创建一个单一的正则表达式来满足所有三个要求。所以,我会对每个condintion进行单独的检查。例如,这个正则表达式检查条件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; 
} 

顺便说一句,请注意,可以重复使用Java中的正则表达式的模式(服务表现增益,因为它是昂贵的编译正则表达式)。

正则表达式不能做你想要的东西(如果我没记错的话)的原因是这个问题需要一个上下文无关语法,而正则表达式是一个正则语法。 Ream more