首页 > 其他 > 详细

Match results Find all passwords

时间:2020-08-22 18:38:58      阅读:82      评论:0      收藏:0      [点我收藏+]

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.");
        }
    }
}

Match results Find all passwords

原文:https://www.cnblogs.com/longlong6296/p/13546513.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!