在 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
regex pattern to find full stop, exclamation mark or question mark in Java
提问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?
那么您的问题更准确地说是什么?