import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Regex {
public static void main(String args[]){
Pattern p = Pattern.compile(".*?(cat).*?(dog)?.*?(tiger)");
String input = "The cat is a tiger";
Matcher m = p.matcher(input);
StringBuffer str = new StringBuffer();
if (m.find()) {
//In the output i want to replace input string with group 3 with group 1 value and group 2 with cow. Though group2 is present or not.
//i.e. group 2 is null
}
}
}
我想知道在java中是否可以使用正则表达式将输入字符串替换为捕获组的特定值。
请帮忙