• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 542
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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