Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Write a program that reads a keyword and some text from the console and prints all sentences from the text, containing that word.
- * A sentence is any sequence of words:
- * ending with dot ('.'), exclamation mark ('!') or question mark ('?').
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class SentenceExtractor {
- public static void main(String[] args) {
- String keyword = "";
- String text = "";
- try {
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(System.in));
- keyword = reader.readLine();
- text = reader.readLine();
- } catch (IOException e) {
- e.printStackTrace();
- }
- String patternStr = String.format("\\b[A-Z][^.?!]*?\\b%s\\b.*?[!.?]", keyword);
- Pattern pattern = Pattern.compile(patternStr);
- Matcher matcher = pattern.matcher(text);
- while (matcher.find()) {
- System.out.println(matcher.group());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement