diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-21 18:58:55 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-21 18:58:55 -0500 |
commit | efc9896da7901421b4e687d377e0cb75e2e99c2a (patch) | |
tree | 553b561ea80157451e899d5212b3fa452ae94210 /src/2020 | |
parent | e82a8cdfe29b3960095848fe9d2fdcbfd39a4ff3 (diff) | |
download | advent-of-code-efc9896da7901421b4e687d377e0cb75e2e99c2a.tar.gz advent-of-code-efc9896da7901421b4e687d377e0cb75e2e99c2a.zip |
slight improvement for regex matching
Diffstat (limited to 'src/2020')
-rw-r--r-- | src/2020/2/mod.rs | 4 | ||||
-rw-r--r-- | src/2020/7/mod.rs | 12 | ||||
-rw-r--r-- | src/2020/8/mod.rs | 4 |
3 files changed, 8 insertions, 12 deletions
diff --git a/src/2020/2/mod.rs b/src/2020/2/mod.rs index 75cc4f1..a2ff9df 100644 --- a/src/2020/2/mod.rs +++ b/src/2020/2/mod.rs @@ -9,9 +9,9 @@ pub struct Line { impl Line { fn parse(line: &str) -> Result<Self> { - let rx = Regex::new(r"^([0-9]+)-([0-9]+) (.): (.*)$").unwrap(); let captures = - rx.captures(line).context("line failed to match regex")?; + regex_captures!(r"^([0-9]+)-([0-9]+) (.): (.*)$", line) + .context("line failed to match regex")?; let c = captures .get(3) .unwrap() diff --git a/src/2020/7/mod.rs b/src/2020/7/mod.rs index 6199ad0..2c8a1a7 100644 --- a/src/2020/7/mod.rs +++ b/src/2020/7/mod.rs @@ -28,11 +28,7 @@ pub fn part2(graph: Graph) -> Result<i64> { } fn parse_line(line: &str) -> Result<(String, Vec<(i64, String)>)> { - let main_rx = Regex::new(r"^(.*) bags contain (.*)\.$").unwrap(); - let contents_rx = Regex::new(r"^([0-9]+) (.*) bags?").unwrap(); - - let captures = main_rx - .captures(line) + let captures = regex_captures!(r"^(.*) bags contain (.*)\.$", line) .context("line failed to match regex")?; let color = captures.get(1).unwrap().as_str(); let contents = captures.get(2).unwrap().as_str(); @@ -44,9 +40,9 @@ fn parse_line(line: &str) -> Result<(String, Vec<(i64, String)>)> { contents .split(", ") .map(|s| { - let captures = contents_rx - .captures(s) - .context("line failed to match regex")?; + let captures = + regex_captures!(r"^([0-9]+) (.*) bags?", s) + .context("line failed to match regex")?; Ok(( captures .get(1) diff --git a/src/2020/8/mod.rs b/src/2020/8/mod.rs index d1908cd..af08f5c 100644 --- a/src/2020/8/mod.rs +++ b/src/2020/8/mod.rs @@ -30,8 +30,8 @@ impl std::str::FromStr for Op { type Err = Error; fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { - let rx = Regex::new(r"^([^ ]*) ((?:-|\+)[0-9]+)$").unwrap(); - let captures = rx.captures(s).context("failed to parse line")?; + let captures = regex_captures!(r"^([^ ]*) ((?:-|\+)[0-9]+)$", s) + .context("failed to parse line")?; let ty = captures.get(1).unwrap().as_str().parse()?; let arg = captures .get(2) |