Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Section 12. Localization, Pattern Matching, and Formatting

12. Localization, Pattern Matching, and Formatting

12.1
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VerySimpleGrep {
  public static void main(String[] args) throws IOException {
    // Test file
    FileWriter fw = new FileWriter("test.txt");
    fw.write("To be or not to be.\n");
    fw.write("Only As, no Bs.\n");
    fw.write("A bee movie is not funny.\n");
    fw.write("Bessy was a beautiful beatnik from Bern.\n");
    fw.close();
    String fileName = "test.txt";
    String regexStr = "[bB]e";
    grepFile(fileName, regexStr);
    // Test on this file:
    fileName = "VerySimpleGrep.java";
    regexStr = "[a-zA-Z_$][a-zA-Z0-9_$]*";
    grepFile(fileName, regexStr);

    // Read file name and regex as program arguments:
    fileName = args[0];
    regexStr = args[1];
    grepFile(fileName, regexStr);
  }

  /**
   * Finds and prints matches for the regex in the text file.
   * @param fileName
   * @param regex
   * @throws IOException
   */
  public static void grepFile(String fileName, String regex)
                              throws IOException {
    Pattern pattern = Pattern.compile(regex);
    BufferedReader source = new BufferedReader(new FileReader(fileName));
    int lineNum = 1;
    String line = source.readLine();
    while (line != null ) {
      if (!line.equals("")) {
        List<String> matchList = grepLine(pattern, line);
        if (matchList.size() != 0)
          System.out.println(lineNum + ": " + matchList);
      }
      lineNum++;
      line = source.readLine();
    }
    source.close();
  }

  /**
   * Finds the matches for the pattern in the target.
   * @param pattern
   * @param target
   * @return List<String> with the matches found in the target
   */
  public static List<String> grepLine(Pattern pattern, String target) {
    Matcher matcher = pattern.matcher(target);
    List<String> matchList = new ArrayList<String>();
    while(matcher.find()) {
      int startCharIndex = matcher.start();
      int lastPlus1Index = matcher.end();
      int lastCharIndex = startCharIndex == lastPlus1Index ?
          lastPlus1Index : lastPlus1Index-1;
      String matchedStr = matcher.group();
      matchList.add("(" + startCharIndex + "," + lastCharIndex + ":" +
          matchedStr + ")");
    }
    return matchList;
  }
}

					  

12.2
import static java.lang.System.out;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;

public class CSVReader {
  public static void main(String[] args) throws IOException {
    FileWriter fw = new FileWriter("csv.txt");
    fw.write("2.5,25,250\n");
    fw.write("Hi,Hello,Howdy\n");
    fw.write("2008,2009,2010\n");
    fw.write("one,two,three\n");
    fw.close();
    BufferedReader source = new BufferedReader(new FileReader("csv.txt"));
    readCSV(source, 3);
    source.close();
  }
  /**
   * Reads values in CSV format.
   * @param source
   * @param numOfFields
   * @throws IOException
   */
  public static void readCSV(Readable source,
                             int numOfFields)throws IOException {
    Scanner lexer = new Scanner(source);
    Pattern csvPattern = compileCSVPattern(numOfFields);
    out.println("Pattern: " + csvPattern.pattern());
    Pattern splitPattern = Pattern.compile(",");
    while (lexer.hasNextLine()) {
      // Match fields on the line
      String record = lexer.findInLine(csvPattern);
      if (record != null) {
        // Split the record on the split pattern:
        String[] fields = splitPattern.split(record, numOfFields);
        out.println(Arrays.toString(fields));
      }
      lexer.nextLine();          // Clear line separator to continue.
    }
    IOException ioe = lexer.ioException();
    if (ioe != null)
      throw ioe;
  }

  /**
   * Creates a multiline-mode pattern that corresponds to the number of fields
   * specified in CSV format on each line/record:
   * ([^,]+),...,([^,]+)
   * Alternative regular expressions for CSV:
   * ^([^,]+),...,([^,]+)
   * ([^,]+),...,([^,]+)$
   * ^([^,]+),...,([^,]+)$
   * (.+),...,(.+)
   *
   * @param numOfFields
   * @return Pattern to match all the field values.
   */
  public static Pattern compileCSVPattern(int numOfFields) {
    assert numOfFields >= 1;
    String fieldPattern = "([^,]+)";
    String patternStr = fieldPattern;
    for (int i = 2; i <= numOfFields; i++) {
      patternStr += "," + fieldPattern;
    }
    return Pattern.compile(patternStr, Pattern.MULTILINE);
  }
}

					  


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint