200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Java正则表达式匹配一句英文句子(大写字母开头 结尾有句号)

Java正则表达式匹配一句英文句子(大写字母开头 结尾有句号)

时间:2021-12-06 15:32:47

相关推荐

Java正则表达式匹配一句英文句子(大写字母开头 结尾有句号)

正则表达式:

[A-Za-z]+[A-Za-z0-9_,"#;.() \s]*[.]$或^([A-Z]){1}[^.]*.

测试代码:

import java.util.regex.Matcher;import java.util.regex.Pattern;public class Contain_Test {public static void main(String[] args) {String example1="I am a handsome boy.";String example2="Where there is a will, there is a way.";String regex="[A-Za-z]+[A-Za-z0-9_,\"#;.() \\s]*[.]$";//匹配一句英文语句。String anotherRegex="^([A-Z]){1}[^.]*.";Pattern pattern=pile(regex);Pattern anotherPattern=pile(anotherRegex);Matcher matcher1=pattern.matcher(example1);Matcher matcher2=pattern.matcher(example2);Matcher matcher3=anotherPattern.matcher(example1);Matcher matcher4=anotherPattern.matcher(example2);boolean result1=matcher1.matches();boolean result2=matcher2.matches();boolean result3=matcher3.matches();boolean result4=matcher4.matches();System.out.println("regex's result:");System.out.println(example1+" =>"+result1);System.out.println(example2+" =>"+result2);System.out.println("anotherRegex's result:");System.out.println(example1+" =>"+result3);System.out.println(example2+" =>"+result4);}}

测试结果:

regex's result:I am a handsome boy. =>trueWhere there is a will, there is a way. =>trueanotherRegex's result:I am a handsome boy. =>trueWhere there is a will, there is a way. =>true

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。