/* Look for phone numbers starting with a 0 after a space
character and replace the 0 with "+44 (0)" */
import java.util.regex.*;
public class Reg6 {
public static void main (String [] args) {
String variable1 = "test";
Pattern phone = Pattern.compile("\\s0");
Matcher action = phone.matcher(args[0]);
String worldwide = action.replaceAll(" +44 (0) ");
System.out.println(worldwide);
}
}
如何将variable1
放入replaceAll方法?如果有一个字符串{$ variable_1}我希望用我的variable_1替换它可能吗?
就像在php模板中一样
“{$ test}” - 我想使用在字符串中匹配的名称,找到与此字符串匹配的变量,并将其置于此处,将所有变量全部替换为此变量值
就像在模板中一样
答案 0 :(得分:0)
您不必使用模式或匹配 试试看
String number ="028346";
if(number.startsWith("0"))
System.out.println("+44 (0)" + number.substring(1));
答案 1 :(得分:0)
也许你想要这个:
Pattern varMatch = Pattern.compile("\\{\\$variable_1\\}");
Matcher action = varMatch.matcher(worldwide);
worldwide = action.replaceAll(variable1);
System.out.println(worldwide);