下面参见示例:
package LyfPractice;
import java.util.regex.Pattern;
/**
* Created by fangjiejie on 2017/1/14.
*/
public class Regex {
public static void main(String[] args) {
String reg1="^[0-9]+(\\.[0-9]{2})?$";
//表示整数或者小数。整数位的位数为1到多,小数点后两位
String reg2="^[0-9]+(\\.[0-9]{1,2})?$";
//表示整数或者小数。整数位的位数为1到多,小数点后一位或者两位
String reg3="^[0-9]+(\\.[0-9]{2,})?$";
//表示整数或者小数。整数位的位数为1到多,小数点后2位以上
String reg4="^(0?[1-9]|1[0-2])$";
//一年中12个月份表示,其中一位数字可由本身表示,或前面以0占位,如一月,可表示为1,01
System.out.println(Pattern.matches(reg4,"08"));
String reg5="hello,China";
System.out.println("-------------replaceFirst-------------");
/*******replaceFirst********/
//贪婪模式---右限
System.out.println(reg5.replaceFirst("\\w+","*"));
System.out.println(reg5.replaceFirst("\\w?","*"));
System.out.println(reg5.replaceFirst("\\w*","*"));
//勉强模式---左限
System.out.println(reg5.replaceFirst("\\w+?","*"));
System.out.println(reg5.replaceFirst("\\w??","*"));
System.out.println(reg5.replaceFirst("\\w*?","*"));
/*******replaceAll********/
System.out.println("-------------replaceAll-------------");
//贪婪模式---右限
System.out.println(reg5.replaceAll("\\w+","*"));
System.out.println(reg5.replaceAll("\\w?","*"));
System.out.println(reg5.replaceAll("\\w*","*"));
//勉强模式---左限
System.out.println(reg5.replaceAll("\\w+?","*"));
System.out.println(reg5.replaceAll("\\w??","*"));
System.out.println(reg5.replaceAll("\\w*?","*"));
}
}