View Source Re (re v1.0.1)

Write readable regular expressions in functional style.

examples

Examples

Match subdomains of example.com:

iex> require Re
iex> require Re.Chars
iex> regex =
...>   Re.sequence([
...>     Re.one_or_more(Re.any_of([Re.Chars.any_ascii, Re.any_of('.-_')])),
...>     Re.text(".example.com")
...>   ]) |> Re.compile()
~r/(?:[\\0-\x7f]|\.|\-|_)+\.example\.com/
iex> "hello.example.com" =~ regex
true
iex> "hello.world.example.com" =~ regex
true
iex> "hello.orsinium.dev" =~ regex
false

Link to this section Summary

Types

internal Re representation of regular expressions.

Functions

Match any of the given patters or symbols.

Capture the pattern.

Named capture of the pattern.

Compile Re AST (or string) into native Regex type.

Group (but not capture) the pattern if needed.

Match any symbol in the given range.

Guard for matching the internal Re AST representation.

"Ungreedy" the pattern.

Match anything except the given symbols.

Match one or more repetitions of the pattern.

Match zero or one repetition of the pattern.

Include a raw regex as is into the resulting pattern.

Match exactly N repetitions of the pattern.

Match from at_least to at_most repetitions of the pattern.

Chain multiple patterns together.

Include a text into the resulting pattern. All unsafe symbols will be escaped if necessary.

Convert the given Re AST into a string.

Match zero or more repetitions of the pattern.

Link to this section Types

@opaque re_ast()

internal Re representation of regular expressions.

Link to this section Functions

@spec any_of([re_ast() | String.t() | char()]) :: re_ast()

Match any of the given patters or symbols.

PCRE: [XY] and X|Y

examples

Examples

iex> rex = Re.any_of([Re.text(?a), Re.text(?b)]) |> Re.compile
iex> "a" =~ rex
true
iex> "b" =~ rex
true
iex> "c" =~ rex
false
iex> "a" =~ Re.any_of([?a, ?b]) |> Re.compile
true
@spec capture(any()) :: re_ast()

Capture the pattern.

https://hexdocs.pm/elixir/1.13/Regex.html#module-captures

PCRE: (X)

examples

Examples

iex> rex = Re.sequence([Re.text(?a), Re.capture(Re.Chars.any_digit)]) |> Re.compile
~r/a(\d)/
iex> Regex.run(rex, "a1", capture: :all_but_first)
["1"]
Link to this macro

capture(expr, name)

View Source (macro)
@spec capture(any(), any()) :: re_ast()

Named capture of the pattern.

https://hexdocs.pm/elixir/1.13/Regex.html#module-captures

PCRE: (?P<N>X)

examples

Examples

iex> rex = Re.sequence([Re.text(?a), Re.capture(Re.Chars.any_digit, "number")]) |> Re.compile
~r/a(?P<number>\d)/
iex> Regex.named_captures(rex, "a1")
%{"number" => "1"}
Link to this macro

compile(expr, options \\ "")

View Source (macro)
@spec compile(re_ast() | String.t(), binary() | [term()]) :: any()

Compile Re AST (or string) into native Regex type.

The result can be used with any functions from the Regex module.

https://hexdocs.pm/elixir/1.13/Regex.html#compile!/2

examples

Examples

iex> "1" =~ Re.compile(Re.Chars.any_digit)
true
iex> "a" =~ Re.compile(Re.Chars.any_digit)
false
@spec group(re_ast() | String.t()) :: re_ast()

Group (but not capture) the pattern if needed.

Usually, you don't need to call this function. All other functions call this one when needed.

PCRE: (?:X)

examples

Examples

iex> 'abc' |> Re.raw |> Re.group |> Re.to_string
"(?:abc)"
Link to this macro

in_range(expr1, expr2)

View Source (macro)
@spec in_range(char(), char()) :: re_ast()

Match any symbol in the given range.

PCRE: [X-Y]

examples

Examples

iex> rex = Re.in_range(?a, ?d) |> Re.compile()
~r/[a-d]/
iex> "a" =~ rex
true
iex> "c" =~ rex
true
iex> "d" =~ rex
true
iex> "e" =~ rex
false
@spec is_re(any()) :: any()

Guard for matching the internal Re AST representation.

examples

Examples

  iex> Re.is_re(Re.text("hello"))
  true
  iex> Re.is_re("something else")
  false
  iex> Re.is_re(~r"hi")
  false
@spec lazy(re_ast()) :: re_ast()

"Ungreedy" the pattern.

By default, all patterns greedy and try to match as much as possbile. This function reverts this behavior for the given pattern, making it match as less as possible.

PCRE: X?

examples

Examples

iex> rex = Re.sequence([
...>  Re.text(?a),
...>  Re.Chars.any_digit |> Re.one_or_more() |> Re.capture
...> ]) |> Re.compile()
~r/a(\d+)/
iex> Regex.run(rex, "a111", capture: :all_but_first)
["111"]
iex> rex = Re.sequence([
...>  Re.text(?a),
...>  Re.Chars.any_digit |> Re.one_or_more() |> Re.lazy |> Re.capture
...> ]) |> Re.compile()
~r/a(\d+?)/
iex> Regex.run(rex, "a111", capture: :all_but_first)
["1"]
@spec none_of([char()]) :: re_ast()

Match anything except the given symbols.

PCRE: [^XY]

examples

Examples

iex> "a" =~ Re.none_of('abc') |> Re.compile()
false
iex> "d" =~ Re.none_of('abc') |> Re.compile()
true
Link to this macro

one_or_more(expr)

View Source (macro)
@spec one_or_more(any()) :: re_ast()

Match one or more repetitions of the pattern.

PCRE: X+

examples

Examples

iex> "a" =~ "a" |> Re.text |> Re.one_or_more |> Re.compile()
true
iex> "aaa" =~ "a" |> Re.text |> Re.one_or_more |> Re.compile()
true
iex> "b" =~ "a" |> Re.text |> Re.one_or_more |> Re.compile()
false
iex> "" =~ "a" |> Re.text |> Re.one_or_more |> Re.compile()
false
Link to this macro

optional(expr)

View Source (macro)
@spec optional(any()) :: re_ast()

Match zero or one repetition of the pattern.

PCRE: X?

@spec raw(String.t() | Regex.t()) :: re_ast()

Include a raw regex as is into the resulting pattern.

Can be dangerous. Don't let untrusted users to pass values there. Use Re.text if you need the input text to be escaped.

examples

Examples

iex> "example.com" =~ Re.raw("example.com") |> Re.compile()
true
iex> "examplescom" =~ Re.raw("example.com") |> Re.compile()
true
iex> "examplscom" =~ Re.raw("example.com") |> Re.compile()
false
Link to this macro

repeated(expr, n)

View Source (macro)
@spec repeated(re_ast() | String.t(), integer()) :: re_ast()

Match exactly N repetitions of the pattern.

PCRE: X{N}

examples

Examples

iex> rex = Re.text("ab") |> Re.repeated(2) |> Re.compile
~r/(?:ab){2}/
iex> "ab" =~ rex
false
iex> "abab" =~ rex
true
Link to this macro

repeated(expr, at_least, at_most)

View Source (macro)
@spec repeated(any(), any(), any()) :: re_ast()

Match from at_least to at_most repetitions of the pattern.

PCRE: X{N,M}

Link to this macro

sequence(exprs)

View Source (macro)
@spec sequence([re_ast() | String.t()]) :: re_ast()

Chain multiple patterns together.

PCRE: XY

examples

Examples

iex> rex = Re.sequence([Re.text("a"), Re.Chars.any_digit]) |> Re.compile
iex> "a1" =~ rex
true
iex> "a" =~ rex
false
iex> "1" =~ rex
false
@spec text(String.t() | integer()) :: re_ast()

Include a text into the resulting pattern. All unsafe symbols will be escaped if necessary.

examples

Examples

iex> rex = Re.text("example.com") |> Re.compile()
iex> "example.com" =~ rex
true
iex> "examplescom" =~ rex
false
Link to this macro

to_string(expr)

View Source (macro)
@spec to_string(re_ast() | String.t() | char()) :: String.t()

Convert the given Re AST into a string.

examples

Examples

iex> Re.to_string(Re.Chars.any_digit)
"\\d"
iex> Re.to_string(Re.Chars.any_ascii)
"[\\\\0-\\x7f]"
Link to this macro

zero_or_more(expr)

View Source (macro)
@spec zero_or_more(any()) :: re_ast()

Match zero or more repetitions of the pattern.

PCRE: X*