简体   繁体   中英

Format the Value and multiple zeros using Regex

This is my first question in this biggest community from where I have learnt a lot.

My requirement is like:
Suppose user enters EK1 then output should be EK1 EK01 EK001 EK0001 .
If input is EK23 , output: EK23 EK023 EK0023 .
If input is EK876 , output: EK876 EK0876
If input is EK0002 , output: EK0002
If input is MUSDG EK768 tygft then output should be MUSDG EK768 EK0768 tygft .

This means that whatever user enters as input, the out should should match 2 alphabet and at least one digit.
Then it should start adding zero.
If it is not matching, then it should return the same value as entered.

I have tried this below code but it is failing at one point.
If digit is only one eg EK1 , then it is giving EK1 EK01 EK001 .
But it should give EK1 EK01 EK001 EK0001 .
That is the current output is missing the last combination.

Please refer tp the code below and see if I am doing any wrong.
Please modify the below code or give me suggestion.

package com.abc.common.customersearch.repository.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test1 {
    private static final Pattern zeroPattern = Pattern.compile("[0]");
    private static final String TOKEN_PATTERN = "[a-zA-Z]{2}[0-9]";
    private static final String ONE = "{1}";
    private static final String TWO = "{2}";
    private static final String THREE = "{3}";
    private static final String FOUR = "{4}";

    public static void main(String as[]) {

        String inputToken = null;
        String outputStr = null;

        inputToken ="EK001";
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");

        inputToken = "MUS EK874";
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");

        inputToken ="EK08";
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");

        inputToken ="MUS EK1"; // This is failing
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");

        inputToken ="MUS EK47";
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");

        inputToken ="EK0001";
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");

        inputToken ="E asfddsfsf EF549 dsgffdgfdgfdg";
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");

        inputToken ="AB09";
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");

        inputToken ="EK7 765kj 34h";
        outputStr = flightToken(inputToken);
        System.out.println("["+inputToken+"] >> ["+outputStr+"]");



    }

    public static String flightToken(String inputToken){

        StringBuilder finalToken = new StringBuilder();
        boolean isValidToken = false;
        for (String token : inputToken.split("\\s")) {
            if (isValidToken(token)) {
                finalToken.append(token).append(" ");
                finalToken.append(getPaddedValue(token)).append(" ");
                isValidToken = true;
            } else {
                finalToken.append(token).append(" ");
            }
        }
        if (isValidToken) {
            //System.out.println("parsed token : " + finalToken.toString());
            return finalToken.toString();
        } else {
            //System.out.println("invalid token : " + inputToken);
            return inputToken;
        }

    }


    private static final String getPaddedValue(String toTest) {
        StringBuilder paddedvalue = new StringBuilder();
        int zeroCounter = countOfAvailableZeroInToken(toTest);
        for (int i = 0; i < 3 - zeroCounter; i++) {
            paddedvalue.append(toTest.substring(0, 2));
            for(int j= 0; j<=i; j++){
                paddedvalue.append("0");
            }
            paddedvalue.append(toTest.split("[a-zA-Z]{2}")[1]).append(" ");
            if(paddedvalue.length()-1>=6){
                break;
            }
        }
        return paddedvalue.toString();
    }


    private static final boolean isValidToken(String pattern) {
        if (pattern.matches(TOKEN_PATTERN + FOUR)
                || pattern.matches(TOKEN_PATTERN + THREE)
                || pattern.matches(TOKEN_PATTERN + TWO)
                || pattern.matches(TOKEN_PATTERN + ONE)) {
            return true;
        } else {
            return false;
        }
    }

    private static final int countOfAvailableZeroInToken(String toTest) {
        int count = 0;
        Matcher zeroMatcher = zeroPattern.matcher(toTest);
        while (zeroMatcher.find()) {
            ++count;
        }
        return count;
    }
}

Change your getPaddedValue(String ...) :

private static final String getPaddedValue(String toTest) {
    StringBuilder paddedvalue = new StringBuilder();
    int zeroCounter = countOfAvailableZeroInToken(toTest);
    for (int i = 0; i < 3 - zeroCounter; i++) {
        StringBuilder currValue = new StringBuilder();
        currValue.append(toTest.substring(0, 2));
        for(int j = 0; j <= i; j++){
            currValue.append("0");
        }
        currValue.append(toTest.split("[a-zA-Z]{2}")[1]).append(" ");

        paddedvalue.append(currValue.toString());

        if(currValue.length() > 6) {
            break;
        }
    }
    return paddedvalue.toString();
}

Use a separate StringBuilder within your loop, and use it for the checking.

Output:

[EK001] >> [EK001 EK0001  ]
[MUS EK874] >> [MUS EK874 EK0874  ]
[EK08] >> [EK08 EK008 EK0008  ]
[MUS EK1] >> [MUS EK1 EK01 EK001 EK0001  ]
[MUS EK47] >> [MUS EK47 EK047 EK0047  ]
[EK0001] >> [EK0001  ]
[E asfddsfsf EF549 dsgffdgfdgfdg] >> [E asfddsfsf EF549 EF0549  dsgffdgfdgfdg ]
[AB09] >> [AB09 AB009 AB0009  ]
[EK7 765kj 34h] >> [EK7 EK07 EK007 EK0007  765kj 34h ]
[EK1 AB2] >> [EK1 EK01 EK001 EK0001  AB2 AB02 AB002 AB0002  ]

Change your getPaddedValue(String toTest)

Every time in the loop, your paddedvalue appended a new value, the length() you need to check is

EK01

EK001

EK0001

not

EK1 EK01 EK001 EK0001

For that reason, paddedvalue's length will greater than 6 in second loop, and then break. This makes your wrong result.

You need to get every sub String, and check the length less than 6

private static final String getPaddedValue(String toTest) {
    StringBuilder paddedvalue = new StringBuilder();
    int zeroCounter = countOfAvailableZeroInToken(toTest);
    for (int i = 0; i < 3 - zeroCounter; i++) {
        paddedvalue.append(toTest.substring(0, 2));
        for(int j= 0; j<=i; j++){
            paddedvalue.append("0");
        }
        paddedvalue.append(toTest.split("[a-zA-Z]{2}")[1]).append(" ");

        //change your if condition like this
        String lastPaddedValue = paddedvalue.toString().split("\\s")[paddedvalue.toString().split("\\s").length - 1];
        if(lastPaddedValue.length()-1>=6){
            break;
        }
    }
    return paddedvalue.toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM