[英]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.