這是我在這個最大社區的第一個問題,我從中學到了很多東西。使用正則表達式格式化值和多個零
我的要求是這樣的:
假設用戶輸入EK1
,那麼輸出應該是EK1 EK01 EK001 EK0001
。
如果輸入是EK23
,則輸出:EK23 EK023 EK0023
。
如果輸入是EK876
,輸出:
如果輸入是EK0002
,輸出:EK0002
如果輸入是MUSDG EK768 tygft
然後輸出應爲MUSDG EK768 EK0768 tygft
。
這意味着無論用戶輸入什麼作爲輸入,輸出應該匹配2個字母和至少一個數字。
然後它應該開始加零。
如果它不匹配,那麼它應該返回與輸入相同的值。
我試過下面的代碼,但它在一個點上失敗。
如果數字只是一個例如EK1
,則它給出EK1 EK01 EK001
。
但它應該給EK1 EK01 EK001 EK0001
。
即當前輸出缺少最後一個組合。
請參考下面的代碼,看看我是否做錯了。
請修改下面的代碼或給我建議。您的循環中
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();
}
使用一個單獨的StringBuilder,並用它來檢查:
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;
}
}
「如果數字是隻有一個如EK1,那麼它給'EK1 EK01 EK001',但它應該給'EK1 EK01 EK001'「有什麼區別? – Gosu
對不起。我編輯過它。它應該是EK1 EK01 EK001 EK0001。但是僅限於EK1 EK01 EK001。即EK0001缺失。 – Zeeshan2307