Literals: Regex

Regex

Regular expressions are represented by the Regex class, which is usually created with a literal:

foo_or_bar = /foo|bar/
heeello    = /h(e+)llo/
integer    = /\d+/

A regular expression literal is delimited by / and uses PCRE syntax.

It can be followed by these modifiers:

  • i: ignore case (PCRE_CASELESS)
  • m: multiline (PCRE_MULTILINE)
  • x: extended (PCRE_EXTENDED)

For example

r = /foo/imx

Slashes must be escaped:

slash = /\//

An alternative syntax is provided:

r = %r(regex with slash: /)

To the extent possible under law, the persons who contributed to this workhave waived
all copyright and related or neighboring rights to this workby associating CC0 with it.
https://crystal-lang.org/docs/syntax_and_semantics/literals/regex.html

在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号

意见反馈
返回顶部