HMdeveloper
HMdeveloper

Reputation: 2884

check any text against this pattern:A sentence that consist of two or more questions

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.

Upvotes: 1

Views: 62

Answers (2)

Pedro Pinheiro
Pedro Pinheiro

Reputation: 1069

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.

Upvotes: 1

ACV
ACV

Reputation: 10562

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]+$

Upvotes: 1

Related Questions