Game Development Reference
In-Depth Information
public ConsoleTokenizer(string arguments)
{
Regex tokenizer = new Regex(@”(['“”][^””]+['“”])\s*|([^\s]+)\s*”,
RegexOptions.IgnoreCase |
RegexOptions.Compiled);
MatchCollection matches = tokenizer.Matches(arguments);
List<string> tokenizedList = new List<string>();
for (int matchIndex = 1;
matchIndex < matches.Count - 1;
matchIndex++)
{
tokenizedList.Add(matches[matchIndex].Value);
}
Tokenize(tokenizedList.ToArray());
}
This constructor simply calls the Tokenize method with an array of arguments.
public ConsoleTokenizer(string[] arguments)
{
Tokenize(arguments);
}
The following method is the heart of the tokenizer. It uses a regular expression to
break up a group of arguments into name-value pairs based on the formatting
styles described earlier.
private void Tokenize(string[] arguments)
{
string pattern = @”^([/-]|—){1}(?<name>\w+)([:=])?(?<value>.+)?$”;
Regex tokenizer = new Regex(pattern,
RegexOptions.IgnoreCase |
RegexOptions.Compiled);
char[] trimCharacters = { '“', '\'' };
string currentToken = null;
foreach (string argument in arguments)
{
Match match = tokenizer.Match(argument);
Search Nedrilad ::




Custom Search