Programming issues for really using Java regex for Huntingtons problem

  • To get \s inside a Java string, you will need to write \\s.
  • The way the newline character and the . character of regex interact is arbitrary, so you will need to do something about this because the typical input file will have multiple lines. One possibility is to read in all the characters into a big string and then replace every \n newline character with a space. Another possibility is to read in the input one line at a time and build up the input in a StringBuilder (you can't use String because strings are immutable) and then change the String at the end.
  • Your professor solved this problem using the String matches() method, but remember that matches() matches the entire string, and remember also that your professor is not such a great Java programmer these days. If you want to use partial matches, then you will need to use the java.util.regex Pattern and Matcher classes. To use those classes:
    • Be sure to import java.util.regex.*
    • If your regex is in the String regex, and the text you are searching through is in the String textToSearch, then your preprosessing steps will be:
      • Pattern pattern = Pattern.compil(regex);
      • Matcher matcher = pattern.matcher(input);
    • After that you will use the find() method of Matcher that returns a Boolean and if find() returns true, then you may next want to use the group() method of Matcher to get the substring of textToSearch that matched.
-- Main.sloan - 2012-02-12
Topic revision: r2 - 2012-02-14 - 00:09:49 - Main.sloan
Copyright 2016 The Board of Trustees
of the University of
Helping Women Faculty Advance
Funded by NSF