构建正则表达式模式以匹配句子 [英] Constructing regex pattern to match sentence

查看:118
本文介绍了构建正则表达式模式以匹配句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个正则表达式模式,该模式将匹配以多个或一个制表符和/或空格开头的任何句子. 例如,我希望我的正则表达式模式能够匹配你好,我喜欢正则表达式!" 但是我想弄清楚如何在"hello"之后匹配单词.到目前为止,我有这个:

I'm trying to write a regex pattern that will match any sentence that begins with multiple or one tab and/or whitespace. For example, I want my regex pattern to be able to match " hello there I like regex!" but so I'm scratching my head on how to match words after "hello". So far I have this:

    String REGEX = "(?s)(\\p{Blank}+)([a-z][ ])*";
    Pattern PATTERN = Pattern.compile(REGEX);
    Matcher m = PATTERN.matcher("         asdsada  adf adfah.");
    if (m.matches()) {
        System.out.println("hurray!");
    }

任何帮助将不胜感激.谢谢.

Any help would be appreciated. Thanks.

推荐答案

String regex = "^\\s+[A-Za-z,;'\"\\s]+[.?!]$"

^表示开头为"
\\s表示空白
+表示1个或更多
[A-Za-z,;'"\\s]表示任何字母,,;'"或空白字符
$表示结尾为"

^ means "begins with"
\\s means white space
+ means 1 or more
[A-Za-z,;'"\\s] means any letter, ,, ;, ', ", or whitespace character
$ means "ends with"

这篇关于构建正则表达式模式以匹配句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆