1

I need to check any sentence against the following pattern:

A sentence that consist of two or more questions

So for example the following sentences all matches with this pattern:

 why do you look at me? Are you alright?
 I am sick. How are you?. Well you do not have to answer it. what's up?
 How are you?I am sick.what's up? Well you do not have to answer it. 

so since it was a little bit complicated for me I tried with just recognizing a simple question so I wrote my code this way:

regx:

^[why|who|where|when|how|did|do|were|was|would|will|should|could|can]\\S+?$

Java code:

private static void questionInRow(String commentstr){
     String urlPattern = "^[why|who|where|when|how|did|do|were|was|would|will|should|could|can]\\S+?$";
     Pattern p = Pattern.compile(urlPattern,Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(commentstr);
        if (m.find()) {
            System.out.println("yes");
        }
}

But even when I ran that code it does not work with this sentence:

why I love u?

So first, what is wrong with query and also I do not know how can I define my regular expression to check for this pattern:

A sentence that consist of two or more questions

any help appreciated.

HMdeveloper
  • 2,772
  • 9
  • 45
  • 74
  • 1
    Try this it will match for two or more questions `(.+?\?){2,}` – Arunesh Singh Sep 10 '15 at 17:56
  • 1
    thank u but what if I want to check special cases like just why and what questions? – HMdeveloper Sep 10 '15 at 17:59
  • 1
    @AruneshSingh also can you explain ?\? because I do not understand how it works with cases like this when questions are not in a row for instance How are u? well good. what's up? – HMdeveloper Sep 10 '15 at 18:07
  • 2
    .+?\? matches anything non-greedily that isn't a ?. So any other punctuation. If you looked at the matches in your sentence, they would be 1) How are u? and 2) well good. what's up? – lintmouse Sep 10 '15 at 18:25
  • @AruneshSingh So this should do the same thing as well : (\?\?){2,} and we do not need .+ since we do not have .+ in between two ? and it works but it does not work can u please explain the reason? – HMdeveloper Sep 10 '15 at 18:35
  • Every sentence you have is after a digit and parentheses? like ``1)`` or ``2)``? – Pedro Pinheiro Sep 10 '15 at 18:36
  • @PedroPinheiro acyually I hope I got ur question right I just put the numerical list to make them clear I fixed it please check I do need 1) or 2) or 3) – HMdeveloper Sep 10 '15 at 18:40

2 Answers2

1

Because \\S stands for a non whitespace character. But in your sentence you have space between the words.

Try this:

^[why|who|where|when|how|did|do|were|was|would|will|should|could|can]+[\s\S]+$
ACV
  • 9,964
  • 5
  • 76
  • 81
1

What you are asking is a way to identify with regex if an english sentence contains two or more questions.

The regex I'll present here might help you, but it has a lot of flaws. Not because the regex is not good, but because it's just impossible to create the perfect regex for what you are asking.

The reason for this is that english is not a regular language. So regular expressions (which is a regular grammar) cannot parse english (the same way it can't parse html).

You can try with this:

/
(
.*      #match whatever characters that can be in a sentence
\?      #match the question sign
){2,}   #must occur two or more times
/gx

Demo1. Sentence number 10 in the demo is a flaw.

That regex will match the sentences with two or more questions, but highly biased because the main way to detect the question is just looking for the question mark no matter the context. As a consequence the regex thinks that this is made out of two questions: '?' this sign means question?.

If you want to match only questions that has words like why or what, you can try to use the following:

/
(
.*
\b(why|what)\b
.*
\?
){2,}
/ igx

Demo2. Sentence number 9 in the demo is a flaw.

Community
  • 1
  • 1
Pedro Pinheiro
  • 1,059
  • 14
  • 32