Repetition

Often we find that some part of a string is unimportant, and what is before and after it is all that we really care about. Placing wildcard dots in that in-between part of a regular expression is fine, as long as we know ahead of time how far apart the relevant pieces are; but we can't always count on that. Suppose we want to locate lines of HTML that contain italic text, enclosed between the start-italics tag <i> and the end-italics tag </i>.

But you <i>must</i> pay the rent!

The regex /<i>....<\/i>/ would identify the above line as containing some italicized chunk four letters long. That's too specific to be useful; if that were the best we could do, we'd have to test each line against a whole lot of regular expressions before ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.