Regex
struct Regex
A compiled regular expression for searching Unicode haystacks.
new
pub fn new(pat: string): Regex throws RegexError
Creates a new Regex object.
new
pub fn new(pat: string): Regex throws RegexError
Creates a new Regex object.
Creates a new Regex object.
If the pattern is invalid, throw error.
Example
use std.regex.Regex;
let re = try! Regex.new("[a-z]+");
assert_eq re.is_match("abc"), true;
Captures all matches of a regular expression in a string, return a Captures? type.
Captures all matches of a regular expression in a string, return a Captures? type.
Captures all matches of a regular expression in a string, return a Captures? type.
If the pattern does not match, nil is returned.
use std.regex.Regex;
let re = try! Regex.new("(\\d{4})-(\\d{2})-(\\d{2})");
let caps = re.captures("2010-03-14")!;
assert_eq caps.len(), 4;
assert_eq caps.get(0)?.text, "2010-03-14";
assert_eq caps.get(0)?.start, 0;
assert_eq caps.get(0)?.end, 10;
assert_eq caps.get(1)?.text, "2010";
assert_eq caps.get(1)?.start, 0;
assert_eq caps.get(1)?.end, 4;
assert_eq caps.get(2)?.text, "03";
assert_eq caps.get(2)?.start, 5;
assert_eq caps.get(2)?.end, 7;
assert_eq caps.get(3)?.text, "14";
assert_eq caps.get(3)?.start, 8;
assert_eq caps.get(3)?.end, 10;
Return the first match in the given string.
Return the first match in the given string.
Return the first match in the given string.
If matched, returns a Match object, otherwise returns nil.
use std.regex.Regex;
let re = try! Regex.new("[0-9]+");
let s = "abc123def456";
let m = re.find(s);
assert_eq m?.text, "123";
assert_eq m?.start, 3;
assert_eq m?.end, 6;
let m = re.find("abc");
assert m.is_nil();
Return the all matches in the given string.
Return the all matches in the given string.
Return the all matches in the given string.
If matched, returns a Match objects as array, otherwise returns [].
use std.regex.Regex;
let re = try! Regex.new("[0-9]+");
let s = "abc123def456";
let m = re.find_all(s);
assert_eq m.len(), 2;
assert_eq m[0].text, "123";
assert_eq m[0].start, 3;
assert_eq m[0].end, 6;
assert_eq m[1].text, "456";
assert_eq m[1].start, 9;
assert_eq m[1].end, 12;
let m = re.find_all("abc");
assert_eq m.len(), 0;
Returns whether the regex matches the given string.
Returns whether the regex matches the given string.
Returns whether the regex matches the given string.
use std.regex.Regex;
let re = try! Regex.new("[a-z]+");
assert_eq re.is_match("abc"), true;
assert_eq re.is_match("123"), false;
Replaces the leftmost-first match in the given string with the replacement provided.
Replaces the leftmost-first match in the given string with the replacement provided.
Replaces the leftmost-first match in the given string with the replacement provided.
use std.regex.Regex;
let re = try! Regex.new("[0-9]+");
assert_eq re.replace("abc123def456", "***"), "abc***def456";
Kw Arguments
expand: Iftrue, the replacement string is treated as a Replacement string syntax, otherwice treated as normal string, default:true.
Replacement string syntax
All instances of $ref in the replacement string are replaced with
the substring corresponding to the capture group identified by ref.
ref may be an integer corresponding to the index of the capture group
(counted by order of opening parenthesis where 0 is the entire match)
or it can be a name (consisting of letters, digits or underscores)
corresponding to a named capture group.
If ref isn't a valid capture group (whether the name doesn't exist or
isn't a valid index), then it is replaced with the empty string.
The longest possible name is used. For example, $1a looks up the
capture group named 1a and not the capture group at index 1. To
exert more precise control over the name, use braces, e.g., ${1}a.
To write a literal $ use $$.
For example
use std.regex.Regex;
let re = try! Regex.new("([\\d]+)([\\w]+)");
assert_eq re.replace("abc123def456", "$2 $1"), "abcdef456 123";
assert_eq re.replace("abc123def456", "$2 $1", expand: false), "abc$2 $1";
See also: https://docs.rs/regex/latest/regex/struct.Regex.html#method.replace
replace_all
pub fn replace_all(self, haystack: string, replacement: string, expand: bool = true): string
Repalce all matches in the given string with the replacement provided.
replace_all
pub fn replace_all(self, haystack: string, replacement: string, expand: bool = true): string
Repalce all matches in the given string with the replacement provided.
Repalce all matches in the given string with the replacement provided.
See also: [replace] method, but this method will replace all matches.
use std.regex.Regex;
let re = try! Regex.new("[0-9]+");
let s = "abc123def456";
assert_eq re.replace_all(s, "***"), "abc***def***";
replace_all_with
pub fn replace_all_with(self, haystack: string, replace_fn: |(Captures)|: string): string
Replace the all match in the given string with Closure.
replace_all_with
pub fn replace_all_with(self, haystack: string, replace_fn: |(Captures)|: string): string
Replace the all match in the given string with Closure.
Replace the all match in the given string with Closure.
The closure will be called with caps: regex.Captures? type, if is not matched, caps will be nil.
Then replace given string with the return string of the closure.
use std.regex.{Regex, Captures};
let re = try! Regex.new("([a-z]+)([\\d]+)");
let s = re.replace_all_with("abc123, def456", |caps| {
let s = caps.get(1)!.text;
let n = caps.get(2)!.text;
return `${s.to_uppercase()}-${n}`;
});
assert_eq s, "ABC-123, DEF-456";
replace_with
pub fn replace_with(self, haystack: string, replace_fn: |(Captures)|: string): string
Replace the first match in the given string with Closure.
replace_with
pub fn replace_with(self, haystack: string, replace_fn: |(Captures)|: string): string
Replace the first match in the given string with Closure.
Replace the first match in the given string with Closure.
The closure will be called with caps: regex.Captures? type, if is not matched, caps will be nil.
Then replace given string with the return string of the closure.
use std.regex.{Regex, Captures};
let re = try! Regex.new("([a-z]+)");
let s = re.replace_with("abc123def456", |caps| {
let cap = caps.get(0)!.text;
return cap.to_uppercase();
});
assert_eq s, "ABC123def456";
Split the given string by the regex.
Split the given string by the regex.
Split the given string by the regex.
use std.regex.Regex;
let re = try! Regex.new("[\\s]+");
let s = "a b c\nd e";
let parts = re.split(s);
assert_eq parts.len(), 5;
assert_eq parts[0], "a";
assert_eq parts[1], "b";
assert_eq parts[2], "c";
assert_eq parts[3], "d";
assert_eq parts[4], "e";