java.util.regex.Matcher.appendReplacement()のサンプル

javaコード
package com.arkgame.study;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ReplacementDemo {

public static void main(String[] args) {
String regex = “([a-zA-Z]+[0-9]+)";
Pattern pattern = Pattern.compile(regex);
String strInput = “age45 salary3456 50000 title";
Matcher matcher = pattern.matcher(strInput);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String replacement = matcher.group(1).toUpperCase();
matcher.appendReplacement(sb, replacement);
}
matcher.appendTail(sb);
System.out.println(“置換後文字列:\r\n" + sb.toString());

}

}

結果
置換後文字列:
AGE45 SALARY3456 50000 title

Java

Posted by arkgame