11.6. Converting strings and regular expressions to each other

The fact that regular expressions aren’t strings is easy to absorb at a glance in the case of regular expressions like this:

/[a-c]{3}/

With its special character-class and repetition syntax, this pattern doesn’t look much like any of the strings it matches ("aaa", "aab", "aac", and so forth).

It gets a little harder not to see a direct link between a regexp and a string when faced with a regexp like this:

/abc/

This regexp isn’t the string "abc". Moreover, it matches not only "abc" but any string with the substring "abc" somewhere inside it (like “Now I know my abcs.”). There’s no unique relationship between a string and a similar-looking regexp.

Still, although the visual resemblance ...

Get The Well-Grounded Rubyist, Second Edition 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.