verbalex v0.3.0 Verbalex View Source

Documentation for Verbalex.

Link to this section Summary

Functions

Matches any character. Equivalent to "."

Match any of the characters in a given character class, classes, or string - provided as opts.

Wraps a regex string in a non-capturing group. This facilitates easy application of things like quantifiers or backreferences to the entirety of a given expression.

Wraps a regex string in a capturing group.

Wraps a regex string in a named-capturing group. To be utilised with Regex.named_captures(regex, string, opts \\ []).

Matches a carriage return character. Equivalent to "\r".

Matches a digit (0-9). Equivalent to "\d".

Express the end of a line regex anchor. Translates to '$'.

Express a string literal to be matched exactly and wraps in a non-capturing group. Alias of then/3 for readability semantics, as 'find' is a makes more sense when placed at the beginning of a pipe chain.

Takes a regex string and applies a lookahead condition. Escapes special characters by default.

Takes a regex string and applies a negative lookahead condition. Escapes special characters by default.

Takes a regex string and applies a negative lookbehind condition. Escapes special characters by default.

Takes a regex string and applies a lookbehind condition. Escapes special characters by default.

Matches a linebreak character. Equivalent to "\n".

Express a pattern as being optional to match.

Matches anything but a digit (0-9). Equivalent to "\D".

Matches anything but whitespace character - includes tabs and line breaks.

Matches anything but a 'word', equivalent to [^a-zA-Z0-9_]

Specify the number of expected occurences to be found on a given expression.

Specify the minimum number of expected occurences to be found on a given expression.

Specify the range of expected occurences to be found on a given expression.

Express a pattern will occur one or more times.

Takes in a list of regex strings, and inserts an or operator between each of them. Does not escape characters by default to preference OR-ing complex expressions and encourage composition. Special character escaping is available via the escape: true option.

Express the start of a line regex anchor. Translates to '^'.

Matches a tab character. Equivalent to "\t".

Express a string literal to be matched exactly and wraps in a non-capturing group. Escapes special characters by default. Turn off character escapes with the escape: false option.

Matches a whitespace character - includes tabs and line breaks.

Matches a 'word', equivalent to [a-zA-Z0-9_]

Matches a 'word boundary' - a word bounary is the position between a word and non-word character (0-9A-Za-z_]). It is commonly thought of as matching the beginning or end of a string.

Express a pattern will occur zero or more times.

Link to this section Types

Link to this type

character_class() View Source
character_class() ::
  :alnum
  | :alpha
  | :ascii
  | :blank
  | :cntrl
  | :digit
  | :graph
  | :lower
  | :print
  | :punct
  | :space
  | :upper
  | :word
  | :xdigit

Link to this type

set_opt() View Source
set_opt() ::
  {:class, character_class()}
  | {:classes, [character_class()]}
  | {:string, String.t()}

Link to this type

set_opts() View Source
set_opts() :: [set_opt()]

Link to this section Functions

Link to this function

anything(before \\ "") View Source
anything(binary()) :: binary()

Matches any character. Equivalent to "."

Link to this function

anything_in(before, opts \\ []) View Source
anything_in(binary(), set_opts()) :: binary()

Match any of the characters in a given character class, classes, or string - provided as opts.

The supported class names (provided with class: ..., or classes: [...]) are:

  • :alnum - Letters and digits
  • :alpha - Letters
  • :ascii - Character codes 0-127
  • :blank - Space or tab only
  • :cntrl - Control characters
  • :digit - Decimal digits (same as \d)
  • :graph - Printing characters, excluding space
  • :lower - Lowercase letters
  • :print - Printing characters, including space
  • :punct - Printing characters, excluding letters, digits, and space
  • :space - Whitespace (the same as \s from PCRE 8.34)
  • :upper - Uppercase letters
  • :word - "Word" characters (same as \w)
  • :xdigit - Hexadecimal digits

Examples

iex> alias Verbalex, as: Vlx
iex> "" |> Vlx.anything_in(class: :alnum, string: "._%+-") |> Vlx.one_or_more()
"[[:alnum:]._%+-]+"

iex> alias Verbalex, as: Vlx
iex> "" |> Vlx.anything_in(classes: [:lower, :punct])
"[[:lower:][:punct:]]"
Link to this function

anything_not_in(before, opts \\ []) View Source
anything_not_in(binary(), set_opts()) :: binary()

Inverse of anything_in/2.

Link to this function

atomize(string) View Source
atomize(binary()) :: binary()

Wraps a regex string in a non-capturing group. This facilitates easy application of things like quantifiers or backreferences to the entirety of a given expression.

Examples

iex> alias Verbalex, as: Vlx
iex> protocol = Vlx.find("http") |> Vlx.maybe("s") |> Vlx.then("://") |> Vlx.atomize()
iex> Vlx.maybe(protocol) |> Vlx.then("www.")
"(?:(?:http)s?(?:://))?(?:www\\.)"
Link to this function

atomize(before, string) View Source
atomize(binary(), binary()) :: binary()

Link to this function

capture(before, string, opts \\ []) View Source
capture(binary(), binary(), [{:escape, false}]) :: binary()

Wraps a regex string in a capturing group.

Examples

iex> alias Verbalex, as: Vlx
iex> pattern = Vlx.find("this") |> Vlx.then("that")
iex> Vlx.capture(pattern)
"((?:this)(?:that))"
Link to this function

capture_as(before, as) View Source
capture_as(binary(), binary()) :: binary()

Wraps a regex string in a named-capturing group. To be utilised with Regex.named_captures(regex, string, opts \\ []).

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.find("http") |> Vlx.maybe("s") |> Vlx.then("://") |> Vlx.capture_as("protocols")
"(?<protocols>(?:http)s?(?::\/\/))"
Link to this function

carriage_return(before \\ "") View Source
carriage_return(binary()) :: binary()

Matches a carriage return character. Equivalent to "\r".

Link to this function

digit(before \\ "") View Source
digit(binary()) :: binary()

Matches a digit (0-9). Equivalent to "\d".

Link to this function

end_of_line(before \\ "") View Source
end_of_line(binary()) :: binary()

Express the end of a line regex anchor. Translates to '$'.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.find("far, far, away.") |> Vlx.end_of_line() |> Regex.compile!()
~r/(?:far,\ far,\ away\.)$/
Link to this function

find(before, string, opts \\ []) View Source
find(binary(), binary(), [{:escape, false}]) :: binary()

Express a string literal to be matched exactly and wraps in a non-capturing group. Alias of then/3 for readability semantics, as 'find' is a makes more sense when placed at the beginning of a pipe chain.

NOTE: Under the current implementation, when using the escape: false option in pipe chains beginning with find, an empty string must be provided as the first value.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.find("needle") |> Vlx.if_preceded_by("hay") |> Vlx.if_followed_by("stack")
"(?<=hay)(?:needle)(?=stack)"
Link to this function

if_followed_by(before, string, opts \\ []) View Source
if_followed_by(binary(), binary(), [{:escape, false}]) :: binary()

Takes a regex string and applies a lookahead condition. Escapes special characters by default.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.find("sentence") |> Vlx.if_followed_by(".") |> Vlx.capture()
"((?:sentence)(?=\\.))"
Link to this function

if_not_followed_by(before, string, opts \\ []) View Source
if_not_followed_by(binary(), binary(), [{:escape, false}]) :: binary()

Takes a regex string and applies a negative lookahead condition. Escapes special characters by default.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.find("good") |> Vlx.if_not_followed_by("bye") |> Vlx.capture()
"((?:good)(?!bye))"
Link to this function

if_not_preceded_by(before, string, opts \\ []) View Source
if_not_preceded_by(binary(), binary(), [{:escape, false}]) :: binary()

Takes a regex string and applies a negative lookbehind condition. Escapes special characters by default.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.digit() |> Vlx.if_not_preceded_by("%") |> Vlx.capture()
"((?<!%)\d)"
Link to this function

if_preceded_by(before, string, opts \\ []) View Source
if_preceded_by(binary(), binary(), [{:escape, false}]) :: binary()

Takes a regex string and applies a lookbehind condition. Escapes special characters by default.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.digit() |> Vlx.if_preceded_by("$") |> Vlx.capture()
"((?<=\\$)\d)"
Link to this function

linebreak(before \\ "") View Source
linebreak(binary()) :: binary()

Matches a linebreak character. Equivalent to "\n".

Link to this function

maybe(before, string \\ "", opts \\ []) View Source
maybe(binary(), binary(), [{:escape, false}]) :: binary()

Express a pattern as being optional to match.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.find("colo") |> Vlx.maybe("u") |> Vlx.then("r")
"(?:colo)u?(?:r)"
Link to this function

not_digit(before \\ "") View Source
not_digit(binary()) :: binary()

Matches anything but a digit (0-9). Equivalent to "\D".

Link to this function

not_whitespace(before \\ "") View Source
not_whitespace(binary()) :: binary()

Matches anything but whitespace character - includes tabs and line breaks.

Link to this function

not_word(before \\ "") View Source
not_word(binary()) :: binary()

Matches anything but a 'word', equivalent to [^a-zA-Z0-9_]

Link to this function

occurs(before, n) View Source
occurs(binary(), integer()) :: binary()

Specify the number of expected occurences to be found on a given expression.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.digit() |> Vlx.capture() |> Vlx.occurs(4)
"(\d){4}"
Link to this function

occurs_at_least(before, n) View Source
occurs_at_least(binary(), integer()) :: binary()

Specify the minimum number of expected occurences to be found on a given expression.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.digit() |> Vlx.capture() |> Vlx.occurs_at_least(2)
"(\d){2,}"
Link to this function

occurs_in_range(before, min, max) View Source
occurs_in_range(binary(), integer(), integer()) :: binary()

Specify the range of expected occurences to be found on a given expression.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.digit() |> Vlx.capture() |> Vlx.occurs_in_range(3, 6)
"(\d){3,6}"
Link to this function

one_or_more(before, string \\ "", opts \\ []) View Source
one_or_more(binary(), binary(), [{:escape, false}]) :: binary()

Express a pattern will occur one or more times.

Examples

iex> alias Verbalex, as: Vlx
iex> "" |> Vlx.anything_in(class: :alnum) |> Vlx.one_or_more()
"[[:alnum:]]+"
Link to this function

or_expressions(subexps, string \\ "", opts \\ []) View Source
or_expressions([binary()], binary(), [{:escape, false}]) :: binary()

Takes in a list of regex strings, and inserts an or operator between each of them. Does not escape characters by default to preference OR-ing complex expressions and encourage composition. Special character escaping is available via the escape: true option.

Examples

iex> alias Verbalex, as: Vlx
iex> accepted_exprs = ["match", "any", "of", "these"]
iex> Vlx.or_expressions(accepted_exprs)
"(match|any|of|these)"
Link to this function

start_of_line(before \\ "") View Source
start_of_line(binary()) :: binary()

Express the start of a line regex anchor. Translates to '^'.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.start_of_line() |> Vlx.find("A long time ago...") |> Regex.compile!()
~r/^(?:A\ long\ time\ ago\.\.\.)/

Matches a tab character. Equivalent to "\t".

Link to this function

then(before, string, opts \\ []) View Source
then(binary(), binary(), [{:escape, false}]) :: binary()

Express a string literal to be matched exactly and wraps in a non-capturing group. Escapes special characters by default. Turn off character escapes with the escape: false option.

Examples

iex> alias Verbalex, as: Vlx
iex> Vlx.find("www.") |> Vlx.then("example.com")
"(?:www\\.)(?:example\\.com)"
Link to this function

whitespace(before \\ "") View Source
whitespace(binary()) :: binary()

Matches a whitespace character - includes tabs and line breaks.

Link to this function

word(before \\ "") View Source
word(binary()) :: binary()

Matches a 'word', equivalent to [a-zA-Z0-9_]

Link to this function

word_boundary(before \\ "") View Source
word_boundary(binary()) :: binary()

Matches a 'word boundary' - a word bounary is the position between a word and non-word character (0-9A-Za-z_]). It is commonly thought of as matching the beginning or end of a string.

Link to this function

zero_or_more(before, string \\ "", opts \\ []) View Source
zero_or_more(binary(), binary(), [{:escape, false}]) :: binary()

Express a pattern will occur zero or more times.

Examples

iex> alias Verbalex, as: Vlx
iex> "" |> Vlx.anything_in(string: "#@$%", class: :alnum) |> Vlx.zero_or_more()
"[#@$%[:alnum:]]*"