CHAPTER SIXTEEN
Regular Expressions
Regular expressions provide you with powerful ways to find and modify pat-terns in text – not only short bits of text such as might be entered at a command prompt but also in huge stores of text such as might be found in files on disk. A regular expression takes the form of a pattern which is compared with a string. Regular expressions also provide means by which you can modify strings so that, for example, you might change specific characters by putting them into uppercase; or you might replace every occurrence of ‚Diamond‛ with ‚Ruby‛; or read in a file of programming code, extract all the comments and write out a new documentation file containing all the comments but none of the code. We’ll find out how to write a comment-extraction tool shortly. First, though, let’s take a look at some very simple regular expressions.