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

时间:2012-03-14 12:46:53

标签: java regex

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

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

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

1 个答案:

答案 0 :(得分:7)

这对我有用:

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());
        }
    }

}

那么你的问题更准确的是什么?

相关问题