jtlc/tests/Regex.jtl
josh 01bd481bce initial import
git-svn-id: svn://anubis/jtlc/trunk@2 f5bc74b8-7b62-4e90-9214-7121d538519f
2009-03-28 15:17:48 +00:00

41 lines
1.2 KiB
Plaintext

module Regex;
import std.io.*;
int main(args : String[])
{
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\d+$/i")
{
println(arg, " is a hexadecimal number");
}
else
{
/* auto declaration of matches to be of type String[] */
matches ::= arg =~ "/(.)(.)/";
if (matches)
{
println("first character: ", matches[0],
"second character: ", matches[1]);
}
}
}
return 0;
}