jtlc/tests/Regex.jtl
josh bb982b62b8 modified example programs, added strings to lexer
git-svn-id: svn://anubis/jtlc/trunk@6 f5bc74b8-7b62-4e90-9214-7121d538519f
2010-01-12 20:23:08 +00:00

40 lines
1.2 KiB
Plaintext

module Regex;
import std.io.*;
public main(args : String[]) : int
{
foreach (arg <- args)
{
/* operator=~(String, String) returns an array of String results for
* the matches. When there are no explicit parentheses in a pattern,
* the array returned contains just one string - the entire matched
* content. If the pattern does not match, an empty array is returned.
* An empty array evaluations in a boolean context to false, while an
* array with a nonzero number of arguments evaluations to true.
* Hence, the =~ operator can be used in an if() statement to see if
* the string matches a given pattern
*/
if (arg =~ "/^\d+$/")
{
println(arg, " is a number");
}
else if (arg =~ "/^0x[0-9a-fA-F]+$/i")
{
println(arg, " is a hexadecimal number");
}
else
{
/* auto declaration of matches to be of type String[] */
if (matches ::= arg =~ "/(.)(.)/")
{
println("first character: ", matches[0],
"second character: ", matches[1]);
}
}
}
return 0;
}