6.8 In Summary: Think!
I'd like to end this chapter with a story that illustrates just how much benefit a little
thought can go when using NFA regular expressions. Once when using GNU
Emacs, I wanted a regex to find certain kinds of contractions such as "don't," "I'm,"
"we'll," and so on, but to ignore other situations where a single quote might be
next to a word. I came up with a regex to match a word,
\<\w+
, followed by the
Emacs equivalent of
'([tdm]|re|ll|ve)
. It worked, but I realized that using
\<\w+
was silly when I needed only \w. You see, if there is a \w immediately
before the apostrophe, \w+ is certainly there too, so having the regex check for
something we know is there doesn't add any new information unless I want the
exact extent of the match (which I didn't, I merely wanted to get to the area).
Using \w alone made the regex more than 10 times faster.
Yes, a little thought can go a long way. I hope this chapter has given you a little to
think about.
|