Java 正则表达式
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpression {
public static void main(String[] args) {
String name = "今年是2013年吗?";
String year = "";
Pattern pa = Pattern.compile("[0-9]{4}"); //正则表达式(匹配4个数字)
Matcher m = pa.matcher(name); //name是待匹配的字符串
if (m.find()) { //如果匹配得到
year = m.group(); //符合正则表达式的字符串
} else {
Calendar calendar = Calendar.getInstance();
year = "" + calendar.get(Calendar.YEAR);
}
System.out.println(year);
}
}