Here we see how you can extract all substrings in your string that are separated by whitespace characters. You could also use string Split, but this version is simpler and can also be extended more easily. The example gets all operands and operators from an equation string.
=== Program that tokenizes (C#) ===
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
//
// The equation.
//
string operation = "3 * 5 = 15";
//
// Split it on whitespace sequences.
//
string[] operands = Regex.Split(operation, @"\s+");
//
// Now we have each token.
//
foreach (string operand in operands)
{
Console.WriteLine(operand);
}
}
}
=== Output of the program ===
3
*
5
=
15Notes on tokenizers. Computer programs and languages first undergo lexical analysis and tokenization, which gets all the 'tokens' such as those shown in the output above. This is an effective way to parse computer languages or program output.
댓글 없음:
댓글 쓰기