`
hesihua
  • 浏览: 229245 次
  • 性别: Icon_minigender_2
  • 来自: 武汉
社区版块
存档分类
最新评论

提取月份

 
阅读更多
package regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class tiquMonth {
	public static void main(String[] args) {
		String regEx="([a-zA-Z]+)\\s+[0-9]{1,2},\\s*[0-9]{4}";
		String str="June 26, 1951";
		Pattern p=Pattern.compile(regEx);
		Matcher m=p.matcher(str);
		
		while(!m.find()){
			System.out.println("日期格式错误!");
			return;
			
		}
		
		System.out.println(m.group(1));
		System.out.println(m.group(0));
//		System.out.println(m.group(2));
	}

}

 

执行后的结果为:

 June
June 26, 1951

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics