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
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
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
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"]
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"}
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
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)"
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
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
"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"]
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
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
Match zero or one repetition of the pattern.
PCRE: X?
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
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
Match from at_least to at_most repetitions of the pattern.
PCRE: X{N,M}
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
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
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]"
Match zero or more repetitions of the pattern.
PCRE: X*