编写一个正则表达式:检查一个句子是否以大写字母开头,以句号结尾.

时间:2014-11-10 10:03:11   收藏:0   阅读:331
package 正则表达式;

import java.util.regex.Pattern;

public class Test2 {
	public static void main(String[] args) {
		String len="^[A-Z].*[\\.]$";
		String s1="A line terminator.";
		String s2="Wangdan1600";
		String s3="asdfgh.";
		
		System.out.println(s1.matches(len));//参数应该是regex:regex是用来匹配此字符串的正则表达式 
		System.out.println(Pattern.matches(len, s1));//等效于上一句
		//System.out.println(len.matches(s1));//参数错误!!!
		
		System.out.println(s2.matches(len));
		System.out.println(s3.matches(len));
	}

}

matches

public boolean matches(String regex)
通知此字符串是否匹配给定的正则表达式

此方法调用的 str.matches(regex) 形式与以下表达式产生完全相同的结果:

Pattern.matches(regex, str)


原文:http://blog.csdn.net/u012110719/article/details/40956469

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!