From a340e965e857ff35546a3f6744a8af3ee992903f Mon Sep 17 00:00:00 2001 From: Bahex Date: Wed, 2 Jul 2025 20:32:52 +0300 Subject: [PATCH] refactor(nu-command/parse)!: Return `null` for unmatched capture groups, rather than empty string (#16094) Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com> --- crates/nu-command/src/strings/parse.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/nu-command/src/strings/parse.rs b/crates/nu-command/src/strings/parse.rs index 04ac6d9781..1bc7cfc118 100644 --- a/crates/nu-command/src/strings/parse.rs +++ b/crates/nu-command/src/strings/parse.rs @@ -78,12 +78,12 @@ impl Command for Parse { example: "\"foo! bar.\" | parse --regex '(\\w+)(?=\\.)|(\\w+)(?=!)'", result: Some(Value::test_list(vec![ Value::test_record(record! { - "capture0" => Value::test_string(""), + "capture0" => Value::test_nothing(), "capture1" => Value::test_string("foo"), }), Value::test_record(record! { "capture0" => Value::test_string("bar"), - "capture1" => Value::test_string(""), + "capture1" => Value::test_nothing(), }), ])), }, @@ -398,8 +398,10 @@ fn captures_to_value( .iter() .zip(captures.iter().skip(1)) .map(|(column, match_)| { - let match_str = match_.map(|m| m.as_str()).unwrap_or(""); - (column.clone(), Value::string(match_str, span)) + let match_value = match_ + .map(|m| Value::string(m.as_str(), span)) + .unwrap_or(Value::nothing(span)); + (column.clone(), match_value) }) .collect();