Write a program searching for passwords in a given text. It is known that:
:
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.");
}
}
}
Match results Find all passwords
原文:https://www.cnblogs.com/longlong6296/p/13546513.html