JAVA replaceAll 正则表达式(持续更新)
Java String类自带了replaceAll 函数,支持正则表达式。
-
replaceAll
匹配数字:[0-9]
String tmp = "";
String content = "1: In a newly-released report, IEA said it expected oil prices to start picking up in 2017.";
tmp = content.replaceAll( "[0-9]{1}" , "china");
System.out.println(tmp);
tmp = content.replaceAll( "[0-9]{2}" , "china");
System.out.println(tmp);
tmp = content.replaceAll( "[0-9]{3}" , "china");
System.out.println(tmp);
tmp = content.replaceAll( "[0-9]{4}" , "china");
System.out.println(tmp);
tmp = content.replaceAll( "[0-9]+" , "china");
System.out.println(tmp);
或用符号的正则表达式
String content = "1: In a newly-released report, IEA said it expected oil prices to start picking up in 2017.";
tmp = content.replaceAll( "\\d{1}" , "china");
System.out.println(tmp);
tmp = content.replaceAll( "\\d{2}" , "china");
System.out.println(tmp);
tmp = content.replaceAll( "\\d{3}" , "china");
System.out.println(tmp);
tmp = content.replaceAll( "\\d{4}" , "china");
System.out.println(tmp);
tmp = content.replaceAll( "\\d+" , "china");
System.out.println(tmp);
输出结果:
china: In a newly-released report, IEA said it expected oil prices to start picking up in chinachinachinachina.
1: In a newly-released report, IEA said it expected oil prices to start picking up in chinachina.
1: In a newly-released report, IEA said it expected oil prices to start picking up in china7.
1: In a newly-released report, IEA said it expected oil prices to start picking up in china.
china: In a newly-released report, IEA said it expected oil prices to start picking up in china.