hazartilirot
hazartilirot

Reputation: 355

regular expression by question mark (Java)

I have collection List<String> sentencesList = new ArrayList<String> including sentencses from the text https://gist.github.com/c9afbb46abd50a28ef6e I have to work with sentences by question mark only. So, I'm taking each element in my collection and I'm checking by regular expression

for(String sentence : sentencesList) {
            int count = 0;
            if(sentence.matches("([^.!?]*)\\?")) { ... }
}

In the text are nine sentences by question mark, but it's working only for one:

He himself was a very old man with shaggy white hair which grew over most of his face as well as on his head, and they liked him almost at once; but on the first evening when he came out to meet them at the front door he was so odd-looking that Lucy (who was the youngest) was a little afraid of him, and Edmund (who was the next youngest) wanted to laugh and had to keep on pretending he was blowing his nose to hide it?

I suppose my problem is regular experession. Please, help! Thank's!

P.S. Regular expression is working only when after interrogative sentence are double line feed. :(

Upvotes: 0

Views: 1098

Answers (4)

31113
31113

Reputation: 409

For question mark only:

String regex = "(?<=[?])\\s*"; 

Also you c an use String regex = "(?<=[.!?])\\s*"; for . ! and ?

Upvotes: 0

Brinnis
Brinnis

Reputation: 906

If you have a List of sentences, why not just do :

for(String sentence : sentencesList) {
    if (sentence.contains("?")){
        //Do something
    }
}

Upvotes: 1

Pshemo
Pshemo

Reputation: 124235

I tested your regex this way and it seams to work fine, so it is probably your way of using it or your data are incorrect.

BufferedReader fr = new BufferedReader(new FileReader("d:\\testText.txt"));
String line = null;
List<String> lines = new ArrayList<String>();
while ((line = fr.readLine()) != null)
    lines.add(line);
// System.out.println(lines);
Pattern pattern = Pattern.compile("([^.!?]*)\\?");
for (String tmp : lines) {
    Matcher m = pattern.matcher(tmp);
    while (m.find())
        System.out.println(m.group());
}

Upvotes: 0

Fanny
Fanny

Reputation: 310

All the other sentences cannot match because they are inside other sentences, like: "What's that noise?" said Lucy suddenly. (it would only match one part of it, the sentence actually ends with a dot). And the first sentence seems weird too: Once there were four children whose names were Peter Susan? Edmund and Lucy.

Upvotes: 0

Related Questions