This module contains functions for regular expression matching and substitution.
match(String, RegExp) -> MatchRes
String = RegExp = string()
MatchRes = {match,Start,Length} | nomatch | {error,errordesc()}
Start = Length = integer()
Finds the first, longest match of the regular expression RegExp
in String
. This function searches for the longest possible match and returns the first one found if there are several expressions of the same length. It returns as follows:
{match,Start,Length}
Start
is the starting
position of the match, and Length
is the length of
the matching string.nomatch
{error,Error}
RegExp
.first_match(String, RegExp) -> MatchRes
String = RegExp = string()
MatchRes = {match,Start,Length} | nomatch |
{error,errordesc()}
Start = Length = integer()
Finds the first match of the regular expression RegExp
in String
. This call is
usually faster than match
and it is also a useful way to ascertain that a match exists. It returns as follows:
{match,Start,Length}
Start
is the starting
position of the match and Length
is the length of
the matching string.nomatch
{error,Error}
RegExp
.matches(String, RegExp) -> MatchRes
String = RegExp = string()
MatchRes = {match, Matches} | {error, errordesc()}
Matches = list()
Finds all non-overlapping matches of the
expression RegExp
in String
.
It returns as follows:
{match, Matches}
{Start, Length}
, where Start
is the starting position of the match, and Length
is the length of the matching string.{error,Error}
RegExp
.sub(String, RegExp, New) -> SubRes
String = RegExp = New = string()
SubRes = {ok,NewString,RepCount} | {error,errordesc()}
RepCount = integer()
Substitutes the first occurrence of a substring matching RegExp
in String
with the string New
. A &
in the string New
is replaced by the matched substring of String
. \&
puts a literal &
into the replacement string. It returns as follows:
{ok,NewString,RepCount}
RegExp
is correct. RepCount
is the number of replacements which have been made
(this will be either 0 or 1).{error, Error}
RegExp
.gsub(String, RegExp, New) -> SubRes
String = RegExp = New = string()
SubRes = {ok,NewString,RepCount} | {error,errordesc()}
RepCount = integer()
The same as sub
, except that all non-overlapping
occurrences of a substring matching
RegExp
in String
are replaced by the string New
. It returns:
{ok,NewString,RepCount}
RegExp
is correct. RepCount
is the number of replacements which have been made.{error, Error}
RegExp
.split(String, RegExp) -> SplitRes
String = RegExp = string()
SubRes = {ok,FieldList} | {error,errordesc()}
Fieldlist = [string()]
String
is split into fields (sub-strings) by the
regular expression RegExp
.
If the separator expression is " "
(a single space),
then the fields are separated by blanks and/or tabs and
leading and trailing blanks and tabs are discarded. For all
other values of the separator, leading and trailing blanks
and tabs are not discarded. It returns:
{ok, FieldList}
FieldList
.{error, Error}
RegExp
.sh_to_awk(ShRegExp) -> AwkRegExp
ShRegExp AwkRegExp = string()
SubRes = {ok,NewString,RepCount} | {error,errordesc()}
RepCount = integer()
Converts the sh
type regular expression
ShRegExp
into a full AWK
regular
expression. Returns the converted regular expression
string. sh
expressions are used in the shell for
matching file names and have the following special
characters:
*
?
[...]
-
. If the first character after [
is a
!
, then any character not enclosed is matched.
It may sometimes be more practical to use sh
type
expansions as they are simpler and easier to use, even though they are not as powerful.
RegExp = string()
ParseRes = {ok,RE} | {error,errordesc()}
Parses the regular expression RegExp
and builds the
internal representation used in the other regular expression
functions. Such representations can be used in all of the
other functions instead of a regular expression string. This
is more efficient when the same regular expression is used
in many strings. It returns:
{ok, RE}
if RegExp
is correct and RE
is the internal
representation.{error, Error}
if there is an error in RegExpString
.format_error(ErrorDescriptor) -> string()
ErrorDescriptor = errordesc()
Returns a string which describes the error ErrorDescriptor
returned when there is an error in a regular expression.
The regular expressions allowed here is a subset of the set found
in egrep
and in the AWK
programming language, as
defined in the book, The AWK Programming Language, by
A. V. Aho, B. W. Kernighan, P. J. Weinberger
. They are
composed of the following characters:
c
.
c
.
abc...
Character ranges are specified by a pair of
characters separated by a -
.
abc...
.
r1
or r2
.
r1
and then r2
.
r
s.
r
s.
r
s.
r
.
The escape sequences allowed are the same as for Erlang strings:
\b
\f
\n
\r
\t
\e
\v
\s
\d
\ddd
\c
\\
for backslash,
\"
for ")
To make these functions easier to use, in combination with the
function io:get_line
which terminates the input line with
a new line, the $
characters also matches a string ending
with "...\n"
. The following examples
define Erlang data types:
Atoms [a-z][0-9a-zA-Z_]* Variables [A-Z_][0-9a-zA-Z_]* Floats (\+|-)?[0-9]+\.[0-9]+((E|e)(\+|-)?[0-9]+)?
Regular expressions are written as Erlang strings when used with the functions in this module. This means that any \
or "
characters in a regular expression
string must be written with \
as they are also escape characters for the string. For example, the regular expression string for Erlang floats is:
"(\\+|-)?[0-9]+\\.[0-9]+((E|e)(\\+|-)?[0-9]+)?"
.
It is not really necessary to have the escape sequences as part of the regular expression syntax as they can always be generated directly in the string. They are included for completeness and can they can also be useful when generating regular expressions, or when they are entered other than with Erlang strings.