Regex - Regular Expression [closed]

1

I would like to know why the regular expressions I wrote are not correctly matching.

The expressions are as follows:

  • User name that must be unique and can not contain special characters ( %-$_#@ , for example), numbers or spaces, except endpoint.

    Example of valid username: jair.anderson or jairanderson

  • Password must be at least 8 digits long, at least 1 uppercase character and at least 1 non-alphabetic character (for example, !$#% )

    Regular expression for the username:

    Pattern regexUsername = Pattern.compile("[._^a-z0-9_\._]");
    

    Regular expression for the password:

    Pattern regexPassword = Pattern.compile("[^!$#%].+");
    

    Can anyone tell me why they do not work as expected?

        
  • asked by anonymous 14.01.2016 / 03:18

    2 answers

    3

    The REGEX for name testing should be:

    '[a-zA-Z\.]+'
    

    So you guarantee only letters (not accented) and .

    As for the password I recommend it to be #, but transcribing the part you are interested in from @Sergio answer.

    • (?=.{8,}) to guarantee 8 characters.
    • (?=.+[A-Z]) to guarantee at least one large letter character.
    • (?=.+[^\w]) to guarantee at least one character other than a-zA-Z0-9_
    14.01.2016 / 12:22
    1

    I also did not understand your question very well, but the following Regex answers the rule of your password:

    ((?=.*[A-Z])(?=.*[@#$!%!]).{8,30})
    

    No stretch

    (?=.*[@#$!%!])
    

    You must enclose the special characters that you intend to accept in square brackets

        
    14.01.2016 / 11:04