Sometimes you've just got to tokenize a string.
Let's say you find you need to parse a string based on the tokens it comprises. It's not an uncommon need, but there's more than one way to go about it. What does "token" mean to you in this situation? How can you break a string down into a list of them?
It can be daunting answering such an open-ended question. But not to fear! Here I'll be explaining your best options and how you'll know when to use them. I'm using Python for these examples, but you should feel free to implement the same algorithms in your language of choice.
Tokenization by delimiters via split
By far the most straightforward approach is one that you've surely used before, and that's quickly done in essentially any language. It's to split a string on each occurrence of some delimiter.
Say you were reading a csv file and for each row your tokens were to be the value associated with each column. It's quick, easy, and almost always sufficient to split each line on the commas. Here's a simple example utilizing a string's split method:
But the usefulness of splitting doesn't end there. You could also introduce some regex and define multiple characters as being delimiters.
Unfortunately, delimiters can only get you so far when doing tokenization. What if you needed to break crazier things like "abc123" into separate tokens, one for the letters and one for the numbers? Or maybe you wanted your delimiters to be represented as separate tokens, not to be tossed entirely? You wouldn't be able to do it using a generic split method.
Tokenization by categorization of chars
One option when dealing with tokens that strictly splitting on delimiters won't work is assigning tokens by character categorization. It's still fast, and its flexibility is likely to be enough for most cases.
Here's an example where the characters in a string are iterated through and tokens are assigned by the categories each character falls into. When the category changes, the token made up of accumulated characters so far is added to the list and a new token is begun.
There are a lot of improvements you could make to this tokenization function! For example, you could record which category each token belonged to. You could make the categories more specific, you could implement some flexibility in how they're handled. You could even use regular expressions for verifying whether a current token plus the next character still fits some category. These are left as exercises to the reader.
Tokenization using regular expressions
But there's a much better way to tokenize when you need to do more than the basics. Here you can find a lexical scanner package which uses regular expressions to define what tokens should look like: https://github.com/pineapplemachine/lexscan. ("Lexical scanner" is a fancy word for a tokenizer that would typically be the first step in a lexical analysis, which is how interpreters and compilers make sense of code. But their applications are in no way limited to just those.) Installation would entail placing the repo's lexscan directory somewhere that your code can access it.
It's very handy! Here's a simple example:
Tokens are assigned based on the longest match of the various expressions provided starting at the end of the previous token. The concept is simple, but tokenization doesn't get much better. The functionality really shines when you're using more complex regular expressions. What if you needed to differentiate between integers and floating point numbers? What if you wanted to capture an string literal enclosed in quotes as a single large token? You can do that using a proper scanner and all it takes is the wherewithal to put together a set of regular expressions which do what you need. Here's a more interesting example that looks more like what you would want to use an implementation like this one for.
Of course, splitting a string will always be faster than using regular expressions for tokenizing. Be judicious in deciding which approach to use!