//Programming A to Z //Week 2 - Kate Hartman //using "Star Trek: The Trouble with Tribbles" as source text import java.util.regex.*; import java.io.*; import java.nio.*; import java.nio.channels.*; public class tribbles { public static void main(String[] args) throws IOException { // Create an input stream and file channel // Using first arguemnt as file name to read in FileInputStream fis = new FileInputStream(args[0]); FileChannel fc = fis.getChannel(); // Read the contents of a file into a ByteBuffer ByteBuffer bb = ByteBuffer.allocate((int)fc.size()); fc.read(bb); fc.close(); // Convert ByteBuffer to one long String String content = new String(bb.array()); // one word in paretheses //String regex = "\\(\\w+\\)"; // all words in parentheses //String regex = "\\((\\w|\\s)+\\)"; // name + stage direction //String regex = "\\n(\\w+)\\:\\s\\((\\w|\\s)+\\)"; // tribbles in parentheses //String regex = "\\((\\w|\\s)+tribble(\\w|\\s)*\\)"; // one word questions //String regex = "\\n\\w+?:\\s\\w+?\\?"; // all questions //String regex = "\\n\\w+?:\\s(\\w|\\s)+?\\?"; // all questions & exclamations String regex = "\\n\\w+?:\\s(\\w|\\s)+?(\\?|\\!)"; Pattern p = Pattern.compile(regex); // Compile Regex Matcher m = p.matcher(content); // Create Matcher while (m.find()) { System.out.println(m.group()); } } }