/ Mask delimited user ID / password password with regular expression

/ Mask delimited passwords. But I want to keep the / separated dates.

FindUserPassword.java


package playground;

public class FindUserPassword {

    public static void main(String[] args) {
        String str6 = "D:\\ap\\cp\\0100.bat  -id:249810 11 2019/05/24 guest/guest kf010 kfu0100.sh 10 1 249810";
        String str5 = "D:\\ap\\cp\\0100.bat  -id:249810 11 kf010 kfu0100.sh 10 1 249810";
        String str4 = "D:\\ap\\cp\\0100.bat  -id:249810 11 2019/05/24 guest/guest/ kf010 kfu0100.sh 10 1 249810";
        String str3 = "D:\\ap\\cp\\0100.bat  -id:249810 11 2019/05/24 guest/guest kf010 kfu0100.sh 10 1 249810";
        String str2 = "D:\\ap\\cp\\01.bat  -id:248669 1 20 12010033/19870420yt 0 tr010_bat -u cp 248669";
        String str = "D:\\ap\\cp\\0100.bat  -id:249810 11 2019/05/24 kf010 kfu0100.sh 10 1 249810";

        System.out.println("str2: " + str2);
        
        String[] commands = str2.split(" ");
        StringBuffer out = new StringBuffer();
        for (String command : commands) {
            //if ((command.indexOf("/") != command.lastIndexOf("/")) || (command.indexOf("/") < 0)) {
            if (command.matches("\\d{4}/\\d{2}/\\d{2}") || (command.indexOf("/") < 0)) {
                out.append(command).append(" ");
            } else {
                //out.append(command.replaceAll("/.+?", "/**********")).append(" ");

                int start = command.indexOf("/");
                String replacestr = command.substring(start+1);
                //out.append(command.replace(replacestr, "**********")).append(" ");
                out.append(command.replace(replacestr, "/**********")).append(" ");
            }
        }
        System.out.println(out);
    }

}

Below, during trial and error (I want to do something about str6)

FindUserPassword_renew.java


package playground;
public class FindUserPassword {
        String str6 = "D:\\ap\\cp\\0100.bat  -id:249810 11 2019/05/24 guest/guest kf010 kfu0100.sh 10 1 249810";
        String str5 = "D:\\ap\\cp\\0100.bat  -id:249810 11 kf010 kfu0100.sh 10 1 249810";
        String str4 = "D:\\ap\\cp\\0100.bat  -id:249810 11 2019/05/24 guest/guest/ kf010 kfu0100.sh 10 1 249810";
        String str3 = "D:\\ap\\cp\\0100.bat  -id:249810 11 2019/05/24 guest/guest kf010 kfu0100.sh 10 1 249810";
        String str2 = "D:\\ap\\cp\\01.bat  -id:248669 1 20 12010033/19870420yt 0 tr010_bat -u cp 248669";
        String str = "D:\\ap\\cp\\0100.bat  -id:249810 11 2019/05/24 kf010 kfu0100.sh 10 1 249810";

    public static void main(String[] args) {
        
        String[] commands = {str, str2, str3, str4, str5, str6}; 
        for (String command : commands) {
          if (!command.matches(".+\\d{4}/\\d{2}/\\d{2}.+")) {
            System.out.println(command.replaceAll("/.+? ", "/********** "));
          } else {
              System.out.println(command);
          }
        }

Recommended Posts

/ Mask delimited user ID / password password with regular expression
Regular expression for password
[Ruby] Regular expression for secure password policy setting
Diffed with JSON
Regular expression basics
JS regular expression
Ruby regular expression
/ Mask delimited user ID / password password with regular expression
Import JSON with SolrJ
JSON validation with JSON schema
Handle JSON with minimal-json
^, $ in Rails regular expression
unicode regular expression sample
Regular expression for password
Format JSON with org.json
java regular expression summary
Extract a string starting with a capital letter with a regular expression (Ruby)
Regular expression basics
JS regular expression
Ruby regular expression
Create a user with an empty password on CentOS7
[Ruby/Rails] How to generate a password in a regular expression