clankill3r
clankill3r

Reputation: 9543

regex get sentences that end with a question mark

I got a big file with all my email activity over the last 5 years. I would like to get all my sentences back that have a question mark. I'm not good with regex, i just know a bit of what can be done.

Could someone give me a regex that would work? (java)

If it's possible then i would like to return the bold in the next example:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed amet elementum. Integer nec diam erat, eu consectetur nibh? Cum sociis natoque penatibus et magnis dis parturient montes.

Upvotes: 2

Views: 7816

Answers (2)

Boris Strandjev
Boris Strandjev

Reputation: 46953

Try this code:

    String str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
            + "My second question? Sed amet elementum."
            + "Integer nec diam erat, eu consectetur nibh?"
            + "Cum sociis natoque penatibus et magnis dis parturient montes.";
    Pattern pattern = Pattern.compile("([^.?!]*)\\?");
    Matcher matcher = pattern.matcher(str);
    while(matcher.find())
    {
        System.out.println(matcher.group());
    }

It outputs:

My second question?
Integer nec diam erat, eu consectetur nibh?

Here I define sentence ending as ., ! or ? but you can extend that list easily.

Upvotes: 5

Qtax
Qtax

Reputation: 33918

You could use an expression like: [^.!?]+\?

This would match the desired text in your example at least.

Upvotes: 0

Related Questions