signature REGEX
structure Regex : REGEX
This structure provides an interface to a (subset of) POSIX-compatible
regular expressions.
Note: however, that the functions resulting from this partial
application cannot be pickled.
import structure Regex from "x-alice:/lib/regex/Regex"
import signature REGEX from "x-alice:/lib/regex/REGEX-sig"
signature REGEX =
sig
type match
infix 2 =~
exception Malformed
exception NoSuchGroup
val match : string -> string -> match option
val =~ : string * string -> bool
val groups : match -> string vector
val group : match * int -> string
val groupStart : match * int -> int
val groupEnd : match * int -> int
val groupSpan : match * int -> (int * int)
end
The abstract type of a matching.
indicates that a regular expression not well-formed.
indicates that an access to a group of a match has failed. It does not exists such a group.
returns SOME m if r matches s and NONE otherwise. It raises Malformed if r is not a well-formed regular expression.
The following equivalence holds:
r =~ s = Option.isSome (match r s)
returns a string vector of the given matching m
need a match m and an index i. It raises NoSuchGroup, if i >= Vector.length (groups m) or i < 0.
This structure provides pattern matching with POSIX 1003.2 regular
expressions.
The form and meaning of Extended and Basic regular expressions are
described below. Here R and S denote regular expressions; m and n
denote natural numbers; L denotes a character list; and d denotes a
decimal digit:
| Extended | Meaning |
|---|---|
Some example character lists L:
Remember that backslash (\) must be escaped as "\\" in SML strings.
Example: Match SML integer constant:
match "^~?[0-9]+$" [Extended]
Example: Match SML alphanumeric identifier:
match "^[a-zA-Z0-9][a-zA-Z0-9'_]*$" [Extended]
Example: Match SML floating-point constant:
match "^[+~]?[0-9]+(\\.[0-9]+|(\\.[0-9]+)?[eE][+~]?[0-9]+)$" [Extended]
Example: Match any HTML start tag; make the tag's name into a group:
match "<([[:alnum:]]+)[^>]*>" [Extended]