JAVA里,如何匹配一个多位数?(正则表达式)
如:string = '' int i = 12;"
请问如何使用replace All这个方法,将所有数字都加上一个标签<b></b>,如
变成string = " int i = <b>12</b>"?
又如把字符串“ 33 444 5555”变成" <b>33</b> <b>444</b> <b>5555</b>"
如:string = '' int i = 12;"
请问如何使用replace All这个方法,将所有数字都加上一个标签<b></b>,如
变成string = " int i = <b>12</b>"?
又如把字符串“ 33 444 5555”变成" <b>33</b> <b>444</b> <b>5555</b>"
2.replaceAll方法,是将匹配的字符串内容进行替换,
但是,替换的内容是人为指定的,很难做到与匹配内容相关联。
String result = str.replaceAll("\\b\\d", "<br>");
result = result.replaceAll("\\d\\b", "</br>");
System.out.println(result);
这样不行啊,每次匹配都把一个数字给替换了,
还是split分隔开,在处理吧
String result = str.replaceAll("\\b\\d", "<br>");
result = result.replaceAll("\\d\\b", "</br>");
System.out.println(result);
这样不行啊,每次匹配都把一个数字给替换了,
还是split分隔开,在处理吧
String result = str.replaceAll("\\b\\d", "<br>");
result = result.replaceAll("\\d\\b", "</br>");
System.out.println(result);
这样不行啊,每次匹配都把一个数字给替换了,
还是split分隔开,在处理吧
貌似方法有点...
求其他方法。
public static void main(String[] args) {
String a=" 33 444 5555";
String t= " int i = 12";
System.out.println(a.replaceAll("(\\d+)", "<b>$1</b>"));
System.out.println(t.replaceAll("(\\d+)", "<b>$1</b>"));
}
}