0

名前は何?ごきげんよう?私は邪魔です。どこにいるの?お腹がすきましたか?私はあなたが好き。

上記の段落では、答えは「あなたの名前は何ですか? どこにいますか?」などのすべての質問を選択する必要があります。

Javaで正規表現を使用して上記を達成するにはどうすればよいですか?

4

2 に答える 2

3

わかりました、このコードをテストしたので、今すぐ動作するはずです。Wh単語で自分自身を見つけようとするのではなく、英語で考えられるすべての単語を探しWhます。

String text = "What is your name? How do you do? I am in way. Where are you? Are you hungry? I like you. What about questions that contain a comma, like this one? Do you like my name, Whitney Houston? What is going to happen now, is you are going to do what I say. Is that clear? What's all this then?";

Pattern p = Pattern.compile("(?:Who|What|When|Where|Why|Which|Whom|Whose)(?:'s)?\\s+[^\\?\\.\\!]+\\?");
Matcher m = p.matcher(text);

List<String> questions = new ArrayList<String>();
while (m.find()) questions.add(m.group());

for (String question : questions) System.out.println(question);

で始まる質問がある可能性があることに気付いたので、単語の後Who'sに許可するようになりました。'sWh

于 2013-01-04T16:41:23.520 に答える
1

簡易版(OP例文用)…

    Pattern p = Pattern.compile("Wh[^\\?]*\\?");
    Matcher m = p.matcher(s);
    while (m.find()) {
            System.out.println(m.group());
    }

より高度なマッチング (Wh 語が文頭にあることを確認する) ...

    Pattern p = Pattern.compile("(^|\\?|\\.) *Wh[^\\?]*\\?");
    Matcher m = p.matcher(s);
    while (m.find()) {
            String match = m.group().substring(m.group().indexOf("Wh"));
            System.out.println(match);
    }
于 2013-01-10T11:51:31.853 に答える