easy_regex/
metacharacters.rs

1//! Methods related to metacharacters.
2
3use crate::{settings::Settings, EasyRegex};
4
5impl EasyRegex {
6    /// Creates an ```EasyRegex``` instance starting with the ```\A``` character, asserts position at start of the string.
7    pub fn only_the_beginning() -> Self {
8        EasyRegex("\\A".to_string())
9    }
10
11    /// Adds the ```\b``` metacharacter, asserts position at a word boundary.
12    pub fn word_boundary(self) -> Self {
13        let result = format!("{}\\b", self.0);
14        EasyRegex(result)
15    }
16
17    /// Adds the ```\w``` metacharacter, matches any word character [a-zA-Z0-9_].
18    pub fn word(self, settings: &Settings) -> Self {
19        let result = self.literal("\\w", settings);
20        result
21    }
22
23    /// Adds the ```\w``` metacharacter, matches any non-word character \[^a-zA-Z0-9_\].
24    pub fn non_word(self, settings: &Settings) -> Self {
25        let result = self.literal("\\W", settings);
26        result
27    }
28
29    /// Adds the ```\d``` metacharacter, matches digit character [0-9].
30    pub fn digit(self, settings: &Settings) -> Self {
31        let result = self.literal("\\d", settings);
32        result
33    }
34
35    /// Adds the ```\D``` metacharacter, matches any non-digit character \[^0-9\].
36    pub fn non_digit(self, settings: &Settings) -> Self {
37        let result = self.literal("\\D", settings);
38        result
39    }
40
41    /// Adds the ```\s``` metacharacter, matches any whitespace character [\r\n\t\f\v ].
42    pub fn whitespace(self, settings: &Settings) -> Self {
43        let result = self.literal("\\s", settings);
44        result
45    }
46
47    /// Adds the ```\S``` metacharacter, matches any non-whitespace character \[^\r\n\t\f\v \].
48    pub fn non_whitespace(self, settings: &Settings) -> Self {
49        let result = self.literal("\\S", settings);
50        result
51    }
52
53    /// Adds the ```\B``` metacharacter, asserts position anywhere but NOT at a word boundary.
54    pub fn non_word_boundary(self) -> Self {
55        let result = format!("{}\\B", self.0);
56        EasyRegex(result)
57    }
58
59    /// Adds the ending metacharacter ```\z```, asserts position at the end of the text.
60    pub fn only_the_end(self) -> Self {
61        let result = format!("{}\\z", self.0);
62        EasyRegex(result)
63    }
64}