/* * regex.aikido * * Darwin Language System, * export version: 1.00 * Copyright (c) 2002 Sun Microsystems, Inc. * * Sun Public License Notice * * The contents of this file are subject to the Sun Public License Version 1.0 (the "License"). You * may not use this file except in compliance with the License. A copy of the License is available * at http://www.opensource.org/licenses/sunpublic.php * * The Original Code is Aikido. * The Initial Developer of the Original Code is David Allison on behalf of Sun Microsystems, Inc. * Copyright (C) Sun Microsystems, Inc. 2000-2003. All Rights Reserved. * * * Contributor(s): dallison * * Version: 1.3 * Created by dallison on 4/19/2002 * Last modified by dallison on 03/07/29 */ // how to use regular expressions // example 1: using the raw language facilities // read the file webserver/mime.types and extract the extensions var lines = System.readfile ("webserver" + System.fileSeparator + "mime.types") foreach line lines { if (line[0] == '#') { // comment? continue } // the regular expression. It means: // 1. subexpression of a sequence of non blanks // 2. a sequence of blanks // 3. a subexpression of any characters const ex = line["([^\t ]+)[\t ]+(.*)"] if (sizeof (ex) == 3) { var mime = line[ex[1].start:ex[1].end] // extract first subex var exts = line[ex[2].start:ex[2].end - 1] // extract second var xt = System.split (exts, ' ') // split at space println ("mime type: " + mime) foreach x xt { println ("\t" + x) } } } // example 2: using the System.match functions foreach line lines { if (line[0] == '#') { // comment? continue } // the regular expression. It means: // 1. subexpression of a sequence of non blanks // 2. a sequence of blanks // 3. a subexpression of any characters var m = System.match (line, "([^\t ]+)[\t ]+(.*)") if (m.nExprs() == 3) { var mime = m.expr(1) // extract first subex var exts = m.expr(2) // extract second var xt = System.split (exts, ' ') // split at space println ("mime type: " + mime) foreach x xt { println ("\t" + x) } } }