std.algorithm.startsWith with maximal matching
Jonathan M Davis
jmdavisProg at gmx.com
Sat Jan 14 19:45:55 PST 2012
On Saturday, January 14, 2012 19:13:02 H. S. Teoh wrote:
> On Fri, Jan 13, 2012 at 09:30:35PM -0800, Jonathan M Davis wrote:
> > On Friday, January 13, 2012 18:47:19 H. S. Teoh wrote:
> [...]
>
> > > But what I really want to accomplish is to parse a string containing
> > > multiple words; at each point I have a list of permitted words that
> > > need to be matched against the string; substring matches don't
> > > count. I already have a way of skipping over spaces; so for medial
> > > words, I can simulate this by appending a space to the end of the
> > > word list passed to startsWith(). However, this doesn't work when
> > > the word being matched is at the very end of the string, or if it is
> > > followed by punctuation.
> > >
> > > Is there another library function that can do this, or do I just
> > > have to roll my own?
> >
> > Use std.array.split. It will split a string into an array of strings
> > using whitespace as the delimiter.
>
> [...]
>
> What about punctuation?
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))
{
//...
}
}
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list