Link to home
Start Free TrialLog in
Avatar of sb1977
sb1977

asked on

How to use a regular expression in Java to validate if 2 identical, consecutive numbers exist

Hello again - I need to do a password validation in Java checking whether or not a string has 2 consecutive, identical characters.  I would like to use a regular expression do this.  I have a working solution in Javascript, but haven't gotten it to work in Java yet.  Here is my Javascript code:

var doesConsIdentCharExist       = /(.)\1/.exec(password);
ASKER CERTIFIED SOLUTION
Avatar of radarsh
radarsh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of radarsh
radarsh

This matches irrespective of whether the character is at the beginning or end or middle and whether
it's alphabetic, numeric or special.

________
radarsh
Avatar of sb1977

ASKER

That works great, thanks again
You're welcome!

Follow some simple rules when converting from a JavaScript RegEx to a Java one.

1. Escape all \ with \\
2. Start and end with .*
3. Use String#replaceAll() method to replace text.

Good Luck!

HEY, THAT GIVES ME MY JAVA CERTIFICATE!!!!!!!!!!
Thank you so much!
________
radarsh
I could solve any problems you may have in C++