v0.4.0

Regex v0.1.0

Source
A compiled regular expression: matching, finding, capturing, splitting and replacing, and escaping text to match it literally.

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.

Source

Fields

NameTypeDescription
startuintThe byte offset of the start of the match.
enduintThe 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

Source

Slice

pub func Slice(&self, text: char8[..]) -> char8[..]

The matched part of text, which must be the text that was searched.

NameTypeDescription
textchar8[..]the text that was searched

Returns: the bytes of the match

Source

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.

Source

Constructors

Regex

pub func Regex(
    allocator: Allocator,
    pattern: char8[..]
) -> Regex throws RegexError

Compiles pattern.

NameTypeDescription
allocatorAllocatorwhat the compiled pattern, and every search of it, takes storage from
patternchar8[..]the pattern, in the syntax Regex describes

Returns: the compiled pattern, or why it could not be compiled and where

Source

Methods

GroupCount

pub func GroupCount(&self) -> uint

How many capturing groups the pattern has, not counting the whole match.

Returns: the count

Source

GroupIndex

pub func GroupIndex(&self, name: char8[..]) -> uint?

The number of the group called name.

NameTypeDescription
namechar8[..]the group's name

Returns: its number, or none when no group has that name

Source

IsMatch

pub func IsMatch(&self, text: char8[..]) -> bool

Whether the pattern matches anywhere in text. Stops at the first match it sees.

NameTypeDescription
textchar8[..]the text to search

Returns: whether there is a match

Source

Find

pub func Find(&self, text: char8[..]) -> Match?

The leftmost match in text; among matches starting there, the one a backtracking matcher would choose.

NameTypeDescription
textchar8[..]the text to search

Returns: the match, or none

Source

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.

NameTypeDescription
textchar8[..]the text to search
startuintthe byte offset to search from, at a character boundary

Returns: the match, or none

Source

Capture

pub func Capture(&self, text: char8[..]) -> Captures?

The leftmost match in text, with where each group matched.

NameTypeDescription
textchar8[..]the text to search

Returns: the groups, or none when there is no match

Source

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.

NameTypeDescription
textchar8[..]the text to search
startuintthe byte offset to search from, at a character boundary

Returns: the groups, or none when there is no match

Source

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.

NameTypeDescription
textchar8[..]the text to search, which must outlive the iterator

Returns: the matches in turn

Source

Split

pub func Split(&self, text: char8[..]) -> SplitIterator

The parts of text between the matches.

NameTypeDescription
textchar8[..]the text to split, which must outlive the iterator

Returns: the parts in turn

Source

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.

NameTypeDescription
allocatorAllocatorwhat the result is kept in
textchar8[..]the text to search
replacementchar8[..]what each match becomes

Returns: the new text

Source

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.

NameTypeDescription
allocatorAllocatorwhat the result is kept in
textchar8[..]the text to search
replacementchar8[..]what the match becomes

Returns: the new text

Source

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.

Source

Implements Iterator

MatchIterator conforms to Iterator.

Next

pub func Next(&var self) -> Match?

The next match, or none after the last.

Returns: the match

Source

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.

Source

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

Source

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.

Source

Methods

Count

pub func Count(&self) -> uint

How many groups there are, counting the whole match as group 0.

Returns: the count

Source

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.

NameTypeDescription
indexuintthe group's number

Returns: where it matched, or none when it did not take part in the match or there is no such group

Source

Named

pub func Named(&self, name: char8[..]) -> Match?

Where the group called name matched.

NameTypeDescription
namechar8[..]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

Source

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.

Source

Parameters

NameTypeDescription
allocatorAllocatorwhat the result is kept in
textchar8[..]the text to escape

Returns: the pattern