이 형식의
당신은 정규식, 수를 증가 사용하여 분석 할 수있다, 다음을 다시 빌드합니다. 이런 식으로 뭔가 : 당신은 코드가 조금 짧게하는 snazzier 정규식과 Matcher.appendReplacement
을 사용할 수 있습니다
public static void main(String[] args) throws Exception {
final Pattern pattern = Pattern.compile("(\\D{4})(\\d{9})(\\D{4})");
final String input = "ABCD000000001XYZL";
final Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
final String head = matcher.group(1);
final long number = Long.parseLong(matcher.group(2)) + 1;
final String tail = matcher.group(3);
final String result = String.format("%s%09d%s", head, number, tail);
System.out.println(result);
}
}
; 복잡성의 대가로 :
public static void main(String[] args) throws Exception {
final Pattern pattern = Pattern.compile("(?<=\\D{4})(\\d{9})(?=\\D{4})");
final String input = "ABCD000000001XYZL";
final Matcher matcher = pattern.matcher(input);
final StringBuffer result = new StringBuffer();
if (matcher.find()) {
final long number = Long.parseLong(matcher.group(1)) + 1;
matcher.appendReplacement(result, String.format("%09d", number));
}
matcher.appendTail(result);
System.out.println(result);
}
String.printf를 살펴보십시오. 또는 MessageFormat. – bmargulies
'ABCD'와'XYZL'은 항상 같습니까? 당신은 하드 코딩 했습니까? –
예 하드 코딩 됨 –