JAVA replaceAll 正则表达式(持续更新)

来源:互联网 发布:互联网金融大数据风控 编辑:程序博客网 时间:2024/05/16 10:22

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.

0 0