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

I am new to regex. Here is a model I could think of:

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

As the documentation says [abc] a, b, or c (simple class)

. But I was somehow wrong.: - (

+3


source to share


1 answer


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 problem more precisely?

+7


source







All Articles