Previous Section  < Free Open Study >  Next Section

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, figs/boxdr.jpg\<\w+figs/boxul.jpg , followed by the Emacs equivalent of figs/boxdr.jpg'([tdm]|re|ll|ve)figs/boxul.jpg . It worked, but I realized that using figs/boxdr.jpg\<\w+figs/boxul.jpg 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.

    Previous Section  < Free Open Study >  Next Section