【问题标题】:regex split text document into sentences正则表达式将文本文档拆分为句子
【发布时间】:2013-07-13 08:19:32
【问题描述】:

我有一个很大的文本字符串,我试图将它拆分为基于“。?!”的句子。但是我的正则表达式无法正常工作,有人可以指导我检测错误吗?

String str = "When my friend said he likes deep dish pizza one day, I immediately set a time to come back to Little Star. Arguably, the best deep dish pizza in SF...though...I don't believe there are many places that do deep dish pizza. That being said...its not the BEST ever, just the best for the area. They use cornmeal in the crust, or on the baking surface, so there's a bit of extra crunch to it. That being said...I'm not sure how much I like the cornmeal texture to my pizza. I kind of want just a GOOD CRUST, you know? No extra stuff to try to make it more crunchy.";
String[] sentences = str.split("/(?<=[.?!])\\S+(?=[a-z])/i");

但它并没有拆分句子。有人可以检测到错误吗?

【问题讨论】:

    标签: java regex split text-segmentation


    【解决方案1】:

    你有错误的正则表达式。 Java 不理解这种 PCRE 类型的正则表达式:

    /(?<=[.?!])\\S+(?=[a-z])/i
    

    使用这个:

    String[] sentences = str.split("(?i)(?<=[.?!])\\S+(?=[a-z])");
    

    【讨论】:

    • 实际上,以下一个有效:str.split("(?
    • 权利也可以,或者你可以使用(?i),就像我上面建议的那样。
    • 带有(?i) 的那个对我不起作用,但@tejas 的修改对我有用。
    【解决方案2】:

    这里有一个小提示:

    斜线与正则表达式没有任何关系

    斜杠是 *some+ 语言的应用程序语言工件。 Java 不是其中之一。

    尝试删除斜线并将结尾的“/i”替换为“(?i)”:

    String[] sentences = str.split("(?i)(?<=[.?!])\\S+(?=[a-z])");
    

    【讨论】:

    • 好的,谢谢。这行得通,稍作修改也行得通: str.split("(?
    • java 可以使用不区分大小写的标志,但语法不同。见编辑
    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2014-11-02
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多