The Java Program: RE_Parse.java

  1 // RE_Parse.java -- break 1st command line arg into key, value pairs
  2 
  3 import java.util.StringTokenizer;
  4 
  5 /*
  6   input looks like this:  "key1=value1; key2=value2; key3=value3"
  7  */
  8 
  9 class RE_Parse {
 10 
 11    // left justify a string by padding with blanks
 12    final static String padding = "                                  ";
 13    public static String fmt (String s, int width) {
 14       final int n = Math.max (width-s.length(), 0);
 15       return (s+padding.substring(0,n));
 16    }
 17 
 18    public static void main (String args[]) {
 19       /*
 20         Split on the character '=' or the character ';' followed by blanks.
 21         NB.  (?: is the start of a non-caputuring group
 22       */
 23       final String [] fields = args[0].split ("(?:=|; *)", -1);  // 
 24       for (int i=0; i<fields.length; i += 2) {
 25          System.out.println (fmt(fields[i],10) + "  " + fields[i+1]);
 26       }
 27    }
 28 }