John Ernest Guadalupe
John Ernest Guadalupe

Reputation: 6639

How to merge two or more conditions in regex in Android Java?

I am using an input filter and have used it to match the regex set in the xml.

So think of this regex that it will test each time the user presses a key in the keyboard.

These are what should match : "john", "john-ernest", "j10n", "jo199", "john-ernest1"

These should not : "-jon", "09john", "aaa", "aa", "john-"

Basically :

I got only up to this : ^[a-zA-Z]+[a-zA-Z0-9-]?[a-zA-Z0-9]*$

Any help would be greatly appreciated The problem with this, it does not accept a '-' character while typing because it only accepts it.

Upvotes: 0

Views: 70

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174826

You may try the below lookahead based regex.

^(?!.*(.)\1)[a-zA-Z]+[a-zA-Z0-9]*(?:-[a-zA-Z0-9]+)*$

Upvotes: 1

Related Questions