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

本文介绍并演示了两种正则表达式的使用方法,用于精确匹配完整的英文句子,包括句子的开头大写字母和结尾的句号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

正则表达式:

[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=Pattern.compile(regex);
        Pattern anotherPattern=Pattern.compile(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.  =>true
Where there is a will, there is a way.  =>true

anotherRegex's result:
I am a handsome boy.  =>true
Where there is a will, there is a way.  =>true

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

越来越胖的GuanRunwei

祝老板们身体健康,财源广进!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值