Regex v0.1.0
Match
pub struct Match {
pub start: uint;
pub end: uint;
}
Where a match is in the text searched: from byte start up to byte end. Copy.
Fields
| Name | Type | Description |
|---|---|---|
start | uint | The byte offset of the start of the match. |
end | uint | The byte offset just past the end of the match. |
Methods
Length
pub func Length(&self) -> uint
How many bytes the match spans.
Returns: the length in bytes
Slice
pub func Slice(&self, text: char8[..]) -> char8[..]
The matched part of text, which must be the text that was searched.
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text that was searched |
Returns: the bytes of the match
Regex
pub struct Regex {
// private fields
}
A compiled pattern. Not copied.
The syntax is RE2's: literals, ., classes such as [a-z], [^0-9], [[:alpha:]], \d, \w and \s,
Unicode general categories and properties such as \p{L}, \p{Lu} and \p{White_Space}, the anchors ^, $,
\A, \z, \b and \B, groups (...), (?:...) and (?<name>...), alternation |, the repetitions *,
+, ? and {n,m} with a lazy ? form of each, and the flags i, m, s and x as (?i) or (?i:...).
Backreferences and lookaround are refused: without them every search takes time linear in the text.
Constructors
Regex
pub func Regex(
allocator: Allocator,
pattern: char8[..]
) -> Regex throws RegexError
Compiles pattern.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | what the compiled pattern, and every search of it, takes storage from |
pattern | char8[..] | the pattern, in the syntax Regex describes |
Returns: the compiled pattern, or why it could not be compiled and where
Methods
GroupCount
pub func GroupCount(&self) -> uint
How many capturing groups the pattern has, not counting the whole match.
Returns: the count
GroupIndex
pub func GroupIndex(&self, name: char8[..]) -> uint?
The number of the group called name.
| Name | Type | Description |
|---|---|---|
name | char8[..] | the group's name |
Returns: its number, or none when no group has that name
IsMatch
pub func IsMatch(&self, text: char8[..]) -> bool
Whether the pattern matches anywhere in text. Stops at the first match it sees.
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to search |
Returns: whether there is a match
Find
pub func Find(&self, text: char8[..]) -> Match?
The leftmost match in text; among matches starting there, the one a backtracking matcher would choose.
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to search |
Returns: the match, or none
FindAt
pub func FindAt(
&self,
text: char8[..],
start: uint
) -> Match?
The leftmost match in text at or after byte start. Anchors and word boundaries still see the text before
start, so ^ does not match there unless start is where a line starts.
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to search |
start | uint | the byte offset to search from, at a character boundary |
Returns: the match, or none
Capture
pub func Capture(&self, text: char8[..]) -> Captures?
The leftmost match in text, with where each group matched.
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to search |
Returns: the groups, or none when there is no match
CaptureAt
pub func CaptureAt(
&self,
text: char8[..],
start: uint
) -> Captures?
The leftmost match in text at or after byte start, with where each group matched.
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to search |
start | uint | the byte offset to search from, at a character boundary |
Returns: the groups, or none when there is no match
FindAll
pub func FindAll(&self, text: char8[..]) -> MatchIterator
Every match in text, leftmost first and never overlapping. An empty match right after another match is
skipped, so a* finds three matches in baab: before the b, aa, and at the end.
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to search, which must outlive the iterator |
Returns: the matches in turn
Split
pub func Split(&self, text: char8[..]) -> SplitIterator
The parts of text between the matches.
| Name | Type | Description |
|---|---|---|
text | char8[..] | the text to split, which must outlive the iterator |
Returns: the parts in turn
Replace
pub func Replace(
&self,
allocator: Allocator,
text: char8[..],
replacement: char8[..]
) -> String
text with every match replaced by replacement, in which $1 or ${1} stands for what group 1 matched,
$name or ${name} for what the group called name matched, $0 for the whole match, and $$ for $. A
group that did not match, or does not exist, stands for nothing.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | what the result is kept in |
text | char8[..] | the text to search |
replacement | char8[..] | what each match becomes |
Returns: the new text
ReplaceFirst
pub func ReplaceFirst(
&self,
allocator: Allocator,
text: char8[..],
replacement: char8[..]
) -> String
text with its first match replaced by replacement, which is read as Replace reads it.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | what the result is kept in |
text | char8[..] | the text to search |
replacement | char8[..] | what the match becomes |
Returns: the new text
MatchIterator
pub struct MatchIterator {
// private fields
}
The matches of a pattern in a text, one after another, leftmost first and never overlapping. Not copied. Borrows the regex and the text, which must outlive it.
Implements Iterator
MatchIterator conforms to Iterator.
Next
pub func Next(&var self) -> Match?
The next match, or none after the last.
Returns: the match
SplitIterator
pub struct SplitIterator {
// private fields
}
The parts of a text between the matches of a pattern. Not copied. Borrows the regex and the text, which must outlive it.
Implements Iterator
SplitIterator conforms to Iterator.
Next
pub func Next(&var self) -> char8[..]?
The next part, or none after the last. There is always one more part than there are matches, so a match at
either end gives an empty part there.
Returns: the part
Captures
pub struct Captures {
// private fields
}
The groups of one match: where each matched, or that it did not. Not copied. Borrows the regex it came from for the group names, so it must not outlive the regex.
Methods
Count
pub func Count(&self) -> uint
How many groups there are, counting the whole match as group 0.
Returns: the count
Group
pub func Group(&self, index: uint) -> Match?
Where group index matched: 0 is the whole match, and the groups count from 1 in the order their ( appear.
| Name | Type | Description |
|---|---|---|
index | uint | the group's number |
Returns: where it matched, or none when it did not take part in the match or there is no such group
Named
pub func Named(&self, name: char8[..]) -> Match?
Where the group called name matched.
| Name | Type | Description |
|---|---|---|
name | char8[..] | the group's name |
Returns: where it matched, or none when it did not take part in the match or there is no such group
Escape
pub func Escape(
allocator: Allocator,
text: char8[..]
) -> String
text with every character a pattern gives a meaning to escaped, so that the result, as a pattern, matches
text and nothing else.
Parameters
| Name | Type | Description |
|---|---|---|
allocator | Allocator | what the result is kept in |
text | char8[..] | the text to escape |
Returns: the pattern