在 Java 中查找句号、感叹号或问号的正则表达式模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9702272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:25:35  来源:igfitidea点击:

regex pattern to find full stop, exclamation mark or question mark in Java

javaregex

提问by Kazekage Gaara

I'm a newbie to regex. Here's the pattern that I could think of :

我是正则表达式的新手。这是我能想到的模式:

Pattern pattern = Pattern.compile("[.!?]");

Pattern pattern = Pattern.compile("[.!?]");

Because the documentationsays [abc] a, b, or c (simple class). But I'm wrong somehow. :-(

因为文档[abc] a, b, or c (simple class). 但不知何故我错了。:-(

采纳答案by Guillaume Polet

This works for me:

这对我有用:

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {

    public static void main(String[] args) throws IOException {
        Pattern pattern = Pattern.compile("[.!?]");
        Matcher m = pattern.matcher("Hello?World!...");
        while (m.find()) {
            System.err.println(m.group());
        }
    }

}

So what is your issue more precisely?

那么您的问题更准确地说是什么?