Using Regular Expressions
Sometimes you want the flexibility of strings, but you need to process them efficiently. Searching and replacing inside of strings can also use a lot of processing cycles. Regular expressions, or regexes, can reduce the costs of string processing substantially thanks to highly optimized processing models. Crystal uses the PCRE syntax.[30] This is implemented as a C binding to the PCRE C-library. (Though Ruby uses a different regex engine, it also uses PCRE syntax.)
Patterns are usually created with the /pattern/ literal syntax. If you have a regex that includes a slash you don’t want to have to escape, you can also create a pattern with %r(pattern). Once you have a pattern, you can apply it to strings. Use =~ or the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access