Match results Find all passwords
Write a program searching for passwords in a given text. It is known that:
- a password consists of digits and Latin upper- and lowercase letters;
- a password always follows the word "password" (it can be written in upper- or lowercase letters), but can be separated from it by any number of whitespaces and colon
:
characters.
Output all passwords found in the text, each password starting with a new line. If the text does not contain any passwords, output "No passwords found." without quotes.
Try to use Matcher
and Pattern
to solve it. All the needed modules are already imported.
Sample Input 1:
My email javacoder@gmail.com with password SECRET115. Here is my old PASSWORD: PASS111.
Sample Output 1:
SECRET115
PASS111
Sample Input 2:
My email is javacoder@gmail.com.
Sample Output 2:
No passwords found.
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
Matcher matcher = Pattern.compile(
"(?i:(?<=password)[\\s:]*[0-9a-z]+)"
).matcher(text);
if (matcher.find()) {
do {
System.out.println(matcher.group().replaceAll("[\\s:]*", ""));
} while (matcher.find());
} else {
System.out.println("No passwords found.");
}
}
}