APIRegex
Regex
Regular expressions, with the syntax and semantics of Go’s regexp
package (RE2): matching runs in time linear in the input, search is
leftmost-first, and lookaround and backreferences are not supported.
There is no callback replacement, byte input, or mutable configuration.
A literal such as /[a-z]+/ or /(?i)hello/ needs no import and is
checked by the compiler; compiled programs build each literal once at
startup. compile builds a pattern at run time instead. Positions are
zero-based, half-open offsets in Unicode scalars, as String.slice uses,
not bytes.
Capture
type Capture = { name : Maybe String , value : Maybe Span }Source
One capture group of a match, numbered from group 1.
Fields
Instances:
Eq Capture
Show Capture
Error
type Error = InvalidPattern StringSource
Why a pattern did not compile.
Constructors
InvalidPattern
InvalidPattern : String -> ErrorSource
The pattern is not valid, for the reason the engine gives.
Instances:
Eq Error
Show Error
Match
type Match = { text : String , start : Int , end : Int , captures : List Capture }Source
One match of a whole pattern.
Fields
Instances:
Eq Match
Show Match
Regex
type RegexSource
A compiled pattern. It is immutable and has no equality or display; use
pattern to see its source.
Span
type Span = { text : String , start : Int , end : Int }Source
A matched piece of the input and where it lies.
Fields
Instances:
Eq Span
Show Span
compile
compile : String -> Result Error RegexSource
Compiles a pattern at run time; each call compiles again. Literal patterns need no call.
import Regex
import Result exposing (Result(..))
valid : String -> Bool
valid source =
case Regex.compile source of
Ok _ -> True
Err _ -> False
valid "[a-z]+"
valid "[a-z" == False
escape
escape : String -> StringSource
The text with every metacharacter quoted, for including literal text in a pattern built at run time.
import Regex
Regex.escape "1+1=2?" == "1\\+1=2\\?"
find
find : Regex -> String -> Maybe MatchSource
The first match in the text, or Nothing.
import Regex
firstWord : String -> Maybe String
firstWord text =
case Regex.find /[a-z]+/ text of
Just found -> Just found.text
Nothing -> Nothing
firstWord "12 abc de" == Just "abc"
firstWord "123" == Nothing
findAll
findAll : Regex -> String -> List MatchSource
Every successive non-overlapping match, in order. An empty match right after a previous match is omitted.
import Regex
starts : List Int
starts = List.map { found -> found.start } (Regex.findAll /[0-9]+/ "a1 b22 c333")
starts == [1, 4, 8]
matches
matches : Regex -> String -> BoolSource
Whether the pattern matches anywhere in the text; anchor it with ^ and
$ to match the whole text.
import Regex
Regex.matches /(?i)hello/ "Say HELLO"
Regex.matches /^[0-9]+$/ "12a" == False
message
message : Error -> StringSource
The reason a pattern did not compile, worded by the engine.
pattern
pattern : Regex -> StringSource
The pattern’s source, without literal delimiters or delimiter escapes.
import Regex
Regex.pattern /a\/b+/ == "a/b+"
replaceAll
replaceAll : Regex -> String -> String -> StringSource
Replaces every match in the text with replacement, in which $1 and
${name} stand for captures and $$ for a dollar sign. A missing group
stands for empty text, and a name takes its longest spelling, so write
${1}x to follow group 1 with x.
import Regex
Regex.replaceAll /(\w+)@(\w+)/ "$2 at $1" "me@host" == "host at me"
Regex.replaceAll /(a)/ "${1}x" "banana" == "baxnaxnax"
replaceAllLiteral
replaceAllLiteral : Regex -> String -> String -> StringSource
Replaces every match in the text with replacement exactly as written.
import Regex
Regex.replaceAllLiteral /[0-9]+/ "$1" "a1b22" == "a$1b$1"
split
split : Regex -> String -> List StringSource
The pieces of the text between matches, as Go’s Regexp.Split gives
them with no limit.
import Regex
Regex.split /,\s*/ "a, b,c" == ["a", "b", "c"]
