[英]Regex to exclude trailing zero with even numbers of digits
我需要一些我要編寫的正則表達式的支持。
我收到了一個始終由8位數字組成的字符串(例如12345678)。 從該字符串中,我需要刪除結尾的零,但始終保持偶數個數字。
因此,例如:
對我而言,最重要的部分是確保保持偶數。
我嘗試使用一些(\\d\\d)+[^(00)]+
但無法實現我想要的功能。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "(00)*$";
final String string = "12345678\n"
+ "12400000\n"
+ "12005600\n"
+ "12340000\n"
+ "12000000\n"
+ "12340000\n"
+ "12345000";
final String subst = "";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
以下是結果
Substitution result: 12345678
1240
120056
1234
12
1234
123450
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.