import java.util.regex.Pattern;
import java.util.regex.Matcher;
/**
时 代 J a v a - nowjava.com 提供
**/
public class Main {
public static void main (String[] args) {
String regex = "\\b\\d+\\b";
StringBuffer sb = new StringBuffer();
String replacementText = "";
String matchedText = "";
String text = "A test test 125 test test this is a test" +
" the value is 100 test test. " +
"This is a test." ;
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);/** 来 自 nowjava.com - 时 代 Java**/
while (m.find()) {
matchedText = m.group();
// Convert the text into an integer for comparing
int num = Integer.parseInt(matchedText);
// Prepare the replacement text
if (num == 100) {
replacementText = "a hundred";
}
else if (num < 100) {
replacementText = "less than a hundred";
}
else {
replacementText = "more than a hundred";
}
m.appendReplacement(sb, replacementText);
}
/**代码未完, 请加载全部代码(NowJava.com).**/
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。