栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

正则表达式匹配一个句子

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

正则表达式匹配一个句子

这将做得很好。我对句子的定义:句子以非空格开头,以句点,感叹号或问号(或字符串结尾)结尾。标点符号后可能会有一个结束语。

[^.!?s][^.!?]*(?:[.!?](?!['"]?s|$)[^.!?]*)*[.!?]?['"]?(?=s|$)

import java.util.regex.*;public class TEST {    public static void main(String[] args) {        String subjectString =         "This is a sentence. " +        "So is "this"! And is "this?" " +        "This is 'stackoverflow.com!' " +        "Hello World";        String[] sentences = null;        Pattern re = Pattern.compile( "# Match a sentence ending in punctuation or EOS.n" + "[^.!?\s]    # First char is non-punct, non-wsn" + "[^.!?]*      # Greedily consume up to punctuation.n" + "(?:          # Group for unrolling the loop.n" + "  [.!?]      # (special) inner punctuation ok ifn" + "  (?!['"]?\s|$)  # not followed by ws or EOS.n" + "  [^.!?]*    # Greedily consume up to punctuation.n" + ")*# Zero or more (special normal*)n" + "[.!?]?       # Optional ending punctuation.n" + "['"]?       # Optional closing quote.n" + "(?=\s|$)",  Pattern.MULTILINE | Pattern.COMMENTS);        Matcher reMatcher = re.matcher(subjectString);        while (reMatcher.find()) { System.out.println(reMatcher.group());        }     }}

这是输出:

This is a sentence.

So is "this"!

And is "this?"

This is 'stackoverflow.com!'

Hello World



正确地匹配所有这些(最后一个句子没有结尾标点符号),看起来似乎并不那么容易!



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569589.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号