• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 540
  • Last Modified:

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);
0
sb1977
Asked:
sb1977
  • 3
1 Solution
 
radarshCommented:
String str="myystr";
System.out.println(str.matches(".*(.)\\1.*"));


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

________
radarsh
0
 
sb1977Author Commented:
That works great, thanks again
0
 
radarshCommented:
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
0
 
QarmazaCommented:
I could solve any problems you may have in C++
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now