40 lines
1.2 KiB
Plaintext
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;
|
|
}
|