题解 | #表示数字#
表示数字
http://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6
看第一个大佬写的 666
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String replaceAll = str.replaceAll("([0-9]+)", "*$1*");
System.out.println(replaceAll);
}
}
记录一下后向引用
引用有问题
([A-Za-z]+)([0-9]+) 字母放前匹配结果没问题,([0-9]+)([A-Za-z]+)反过来就不行
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
String replaceAll = str.replaceAll("([A-Za-z]+)([0-9]+)", "-$1- *$2*"); //正常
// String replaceA = str.replaceAll("([A-Za-z]+)([0-9]+)", " $1 *$2*");
String replace = str.replaceAll("([0-9]+)([A-Za-z]+)", "*$1* $2 "); //有问题
// String replaceAll = str.replaceAll("([0-9]+)", "*$1*");
// String replaceAll = str.replaceAll("([A-Za-z]+)", "*$1*");
System.out.println(replaceAll);
// System.out.println(replaceA);
System.out.println(replace);
}
}