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; }