正则表达式:
"-[0-9]{0,}"
细绳:
"-abc"
根据这里的测试,这不应该发生。我假设我在代码中做错了什么。
代码:
public static void main(String[] args) {
String s = "-abc";
String regex = "-[0-9]{0,}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
if (matcher.group().length() == 0)
break;
// get the number less the dash
int beginIndex = matcher.start();
int endIndex = matcher.end();
String number = s.substring(beginIndex + 1, endIndex);
s = s.replaceFirst(regex, "negative " + number);
}
System.out.println(s);
}
一些上下文:我使用的语音合成程序无法发音带有前导负号的数字,因此必须将其替换为“负”一词。