lang.regexp
ballerina/lang.regexp Ballerina library
Module overview
The lang.regexp
module corresponds to the regexp
basic type.
Functions
find
Returns the first match of a regular expression within a string.
string:RegExp r = re `World`; r.find("Not A Match") is () ⇒ true r.find("Hello World") is regexp:Span ⇒ true r.find("Hello World", 6) is regexp:Span ⇒ true
Parameters
- re RegExp - the regular expression
- str string - the string in which to look for a match of
re
- startIndex int (default 0) - the index within
str
at which to start looking for a match
Return Type
- Span? - a
Span
describing the match, or nil if no match was found
findAll
Returns a list of all the matches of a regular expression within a string. After one match is found, it looks for the next match starting where the previous match ended, so the list of matches will be non-overlapping.
string:RegExp r = re `[bB].tt[a-z]*`; r.findAll("Not A Match").length() ⇒ 0 r.findAll("Butter was bought by Betty but the butter was bitter.").length() ⇒ 4 r.findAll("Butter was bought by Betty but the butter was bitter.", 7).length() ⇒ 3
Parameters
- re RegExp - the regular expression
- str string - the string in which to look for matches of
re
- startIndex int (default 0) - the index within
str
at which to start looking for matches
Return Type
- Span[] - a list containing a
Span
for each match found
findAllGroups
Returns the Groups
of all the matches of a regular expression within a string.
After one match is found, it looks for the next match starting where the previous
match ended, so the list of matches will be non-overlapping.
string:RegExp r = re `(([a-z]u)(bble))`; r.findAllGroups("Not A Match").length() ⇒ 0 r.findAllGroups("rubble, trouble, bubble, hubble").length() ⇒ 3 r.findAllGroups("rubble, trouble, bubble, hubble", 7).length() ⇒ 2
Parameters
- re RegExp - the regular expression
- str string - the string in which to look for matches of
re
- startIndex int (default 0) - the index within
str
at which to start looking for matches
Return Type
- Groups[] - a list containing a
Group
for each match found
findGroups
Returns the Groups
for the first match of a regular expression within a string.
string:RegExp r = re `([bB].tt[a-z]*)`; r.findGroups("Not A Match") is () ⇒ true r.findGroups("Butter was bought by Betty but the butter was bitter.") is regexp:Groups ⇒ true r.findGroups("Butter was bought by Betty but the butter was bitter.", 7) is regexp:Groups ⇒ true
Parameters
- re RegExp - the regular expression
- str string - the string in which to look for a match of
re
- startIndex int (default 0) - the index within
str
at which to start looking for a match
Return Type
- Groups? - a
Groups
list describing the match, or nil if no match was found
fromString
Constructs a regular expression from a string.
The syntax of the regular expression is the same as accepted by the re
tagged data template expression.
regexp:fromString("AB+C*D{1,4}") ⇒ re `AB+C*D{1,4}` regexp:fromString("AB+^*") ⇒ error
Parameters
- str string - the string representation of a regular expression
fullMatchGroups
Returns the Groups
of the match of a regular expression that is a full match of a string.
A match of the regular expression in a string is a full match if it
starts at index 0 and ends at index n
, where n
is the length of the string.
string:RegExp r = re `([0-9]+)×([0-9]+)`; r.fullMatchGroups("test: 1440×900") is () ⇒ true r.fullMatchGroups("1440×900") is regexp:Groups ⇒ true
Parameters
- re RegExp - the regular expression
- str string - the string in which to look for a match of
re
Return Type
- Groups? - a
Groups
list describing the match, or nil if there is not a full match; the firstSpan
in the list will be all ofstr
isFullMatch
Tests whether there is full match of regular expression with a string.
A match of a regular expression in a string is a full match if it
starts at index 0 and ends at index n
, where n
is the length of the string.
string:RegExp r = re `A|Th.*ch|^`; r.isFullMatch("This is a Match") ⇒ true r.isFullMatch("Not a complete Match") ⇒ false
Return Type
- boolean - true if there is full match of
re
withstr
, and false otherwise
matchAt
Tests whether there is a match of a regular expression at a specific index in the string.
string:RegExp r = re `World`; r.matchAt("Hello World") is () ⇒ true r.matchAt("Hello World", 6) is regexp:Span ⇒ true
Parameters
- re RegExp - the regular expression
- str string - the string in which to look for a match of
re
- startIndex int (default 0) - the index within
str
at which to look for a match; defaults to zero
Return Type
- Span? - a
Span
describing the match, or nil ifre
did not match at that index; the startIndex of theSpan
will always be equal tostartIndex
matchGroupsAt
Returns the Groups
of the match of a regular expression at a specific index in the string.
string:RegExp r = re `([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])?`; r.matchGroupsAt("time: 14:35:59") is () ⇒ true r.matchGroupsAt("time: 14:35:59", 6) is regexp:Groups ⇒ true
Parameters
- re RegExp - the regular expression
- str string - the string in which to look for a match of
re
- startIndex int (default 0) - the index within
str
at which to look for a match; defaults to zero
Return Type
- Groups? - a
Groups
list describing the match, or nil ifre
did not match at that index; the startIndex of the firstSpan
in the list will always be equal to thestartIndex
of the first member of the list
replace
function replace(RegExp re, string str, Replacement replacement, int startIndex) returns string
Replaces the first match of a regular expression.
string:RegExp r = re `0+`; r.replace("10010011", "*") ⇒ 1*10011 r.replace("10010011", "*", 4) ⇒ 1001*11 r.replace("122111", "*") ⇒ 122111 r.replace("10010011", replaceFunction) ⇒ 1*10011 r.replace("10010011", replaceFunction, 4) ⇒ 1001*11 isolated function replaceFunction(regexp:Groups groups) returns string => "*";
Parameters
- re RegExp - the regular expression
- str string - the string in which to perform the replacements
- replacement Replacement - a
Replacement
that gives the replacement for the match
- startIndex int (default 0) - the index within
str
at which to start looking for a match; defaults to zero
Return Type
- string -
str
with the first match, if any, replaced by the string specified byreplacement
replaceAll
function replaceAll(RegExp re, string str, Replacement replacement, int startIndex) returns string
Replaces all matches of a regular expression. After one match is found, it looks for the next match starting where the previous match ended, so the matches will be non-overlapping.
string:RegExp r = re `0+`; r.replaceAll("10010011", "*") ⇒ 1*1*11 r.replaceAll("10010011", "*", 4) ⇒ 1001*11 r.replaceAll("122111", "*") ⇒ 122111 r.replaceAll("10010011", replaceFunction) ⇒ 121211 r.replaceAll("10010011", replaceFunction, 4) ⇒ 1001211 isolated function replaceFunction(regexp:Groups groups) returns string => groups[0].substring().length().toString();
Parameters
- re RegExp - the regular expression
- str string - the string in which to perform the replacements
- replacement Replacement - a
Replacement
that gives the replacement for each match
- startIndex int (default 0) - the index within
str
at which to start looking for matches; defaults to zero
Return Type
- string -
str
with every match replaced by the string specified byreplacement
split
Splits a string into substrings separated by matches of a regular expression.
This finds the the non-overlapping matches of a regular expression and
returns a list of substrings of str
that occur before the first match,
between matches, or after the last match. If there are no matches, then
[str]
will be returned.
string:RegExp r = re `,`; r.split("abc,cde,efg") ⇒ ["abc","cde","efg"] r.split("abc cde efg") ⇒ ["abc cde efg"]
Parameters
- re RegExp - the regular expression that specifies the separator
- str string - the string to be split
Return Type
- string[] - a list of substrings of
str
separated by matches ofre
Object types
lang.regexp: Span
A span of a string. A span is a substring of another string.
substring
function substring() returns string
Returns a string with the content of the span.
Fields
- startIndex int - The index within the string where the span starts.
- endIndex int - The index within the string following the end of the span.
The length of the span is
endIndex - startIndex
.
Union types
lang.regexp: Replacement
Replacement
The replacement for the match of a regular expression found within a string. A string value specifies that the replacement is a fixed string. A function that specifies that the replacement is constructed by calling a function for each match.
Intersection types
lang.regexp: Groups
Groups
A list providing detailed information about the match of a regular expression within string.
Each member of the list identifies the Span
within the string matched
by each of the regular expression's capturing groups.
The member with index 0 corresponds to the entire regular expression.
The group with index i, where i > 1,is the i-th capturing group;
this will be nil if the match of the regular expression did not use
a match of the capturing group.
The capturing groups within a regular expression are ordered by the position
of their opening parenthesis.
Function types
Any types
lang.regexp: RegExp
RegExp
The type RegExp refers to the tagged data basic type with tag re
.