std.algorithm.startsWith with maximal matching

Jonathan M Davis jmdavisProg at gmx.com
Sat Jan 14 19:53:10 PST 2012


On Saturday, January 14, 2012 19:45:55 Jonathan M Davis wrote:
> If you have to worry about punctuation, then == isn't going to work. You'll
> need to use some other combination of functions to strip the punctuation
> from one or both ends of the word. One possible solution would be something
> like
> 
> foreach(word; splitter!(std.uni.isWhite)(str))
> {
>     auto found = find!(not!(std.uni.isPunctuation))(word);
>     if(found.startsWith(listOfWords))
>     {
>         //...
>     }
> }

Actually, if the word has to match exactly, then startsWith isn't going to cut 
it. What you need to do is outright strip the punctuation from both ends. 
You'd need something more like

word = find!(not!(std.uni.isPunctuation))(word);
word = array(until!(std.uni.isPunctuation)(word));

if(canFind(wordList, word))
{
    //...
}

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list