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

时间:2022-01-14 22:28:17

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 documentation says [abc] a, b, or c (simple class) . But I'm wrong somehow. :-(

因为文档说[abc] a,b或c(简单类)。但我不知何故错了。 :-(

1 个解决方案

#1


7  

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?

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

#1


7  

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?

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