Regex - Regular Expression

Asked

Viewed 253 times

2

I wonder why the regular expressions I’ve written aren’t giving match (marrying) correctly.

The expressions are as follows::

  1. User name that must be unique and cannot contain special characters (%-$_#@, for example), numbers or spaces, with the exception of endpoint.

    Example of valid user name: jair.anderson or jairanderson

  2. Password must have at least 8 digits, at least 1 uppercase character and at least 1 non-alphabetic character (for example: !$#%)

Regular expression for user name:

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

Regular expression for password:

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

Could someone tell me why they don’t function as expected?

  • How can I validate these rules by regular expression? And why is the expression I made wrong?

  • 1

    Anderson, please click [Edit] below the question and clarify the problem... I was even going to include this last comment of yours that I think is important and I was also going to change the title to be more descriptive, the problem is that I don’t understand your first sentence, wouldn’t it be "For some reason I don’t know, this expression DOESN’T validate the way I need it."???

  • I’m sorry I didn’t ask the question more clearly, but I fixed it, see if you can better understand my problem.

2 answers

3

The REGEX for the name test should be :

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

So you guarantee only letters (not accented) and the .

How much the password I recommend this question, but transcribing the part that interests him reply by @Sergio.

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

1

I also didn’t quite understand your question, but the following Regex meets your password rule:

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

In the stretch

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

You must place between brackets the special characters you intend to accept

Browser other questions tagged

You are not signed in. Login or sign up in order to post.