Change str replace to match substring by default (#10038)

This commit is contained in:
Jakub Žádník 2023-08-18 00:18:16 +03:00 committed by GitHub
parent f33b60c001
commit 2e0fb7c1a6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 34 deletions

View file

@ -4,8 +4,8 @@ use nu_engine::CallExt;
use nu_protocol::{ use nu_protocol::{
ast::{Call, CellPath}, ast::{Call, CellPath},
engine::{Command, EngineState, Stack}, engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Type, report_error_new, Category, Example, PipelineData, ShellError, Signature, Span, Spanned,
Value, SyntaxShape, Type, Value,
}; };
struct Arguments { struct Arguments {
@ -59,12 +59,17 @@ impl Command for SubCommand {
) )
.switch( .switch(
"string", "string",
"match the pattern as a substring of the input, instead of a regular expression", "DEPRECATED option, will be removed in 0.85. Substring matching is now the default.",
Some('s'), Some('s'),
) )
.switch(
"regex",
"match the pattern as a regular expression in the input, instead of a substring",
Some('r'),
)
.switch( .switch(
"multiline", "multiline",
"multi-line regex mode: ^ and $ match begin/end of line; equivalent to (?m)", "multi-line regex mode (implies --regex): ^ and $ match begin/end of line; equivalent to (?m)",
Some('m'), Some('m'),
) )
.allow_variants_without_examples(true) .allow_variants_without_examples(true)
@ -91,7 +96,19 @@ impl Command for SubCommand {
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 2)?; let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 2)?;
let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths); let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
let literal_replace = call.has_flag("no-expand"); let literal_replace = call.has_flag("no-expand");
let no_regex = call.has_flag("string"); if call.has_flag("string") {
report_error_new(
engine_state,
&ShellError::GenericError(
"Deprecated option".into(),
"`str replace --string` is deprecated and will be removed in 0.85.".into(),
Some(call.head),
Some("Substring matching is now the default. Use `--regex` or `--multiline` for matching regular expressions.".into()),
vec![],
),
);
}
let no_regex = !call.has_flag("regex") && !call.has_flag("multiline");
let multiline = call.has_flag("multiline"); let multiline = call.has_flag("multiline");
let args = Arguments { let args = Arguments {
@ -109,19 +126,29 @@ impl Command for SubCommand {
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![ vec![
Example { Example {
description: "Find and replace contents with capture group", description: "Find and replace the first occurrence of a substring",
example: "'my_library.rb' | str replace '(.+).rb' '$1.nu'", example: r"'c:\some\cool\path' | str replace 'c:\some\cool' '~'",
result: Some(Value::test_string("my_library.nu")), result: Some(Value::test_string("~\\path")),
}, },
Example { Example {
description: "Find and replace all occurrences of find string", description: "Find and replace all occurrences of a substring",
example: "'abc abc abc' | str replace -a 'b' 'z'", example: r#"'abc abc abc' | str replace -a 'b' 'z'"#,
result: Some(Value::test_string("azc azc azc")), result: Some(Value::test_string("azc azc azc")),
}, },
Example { Example {
description: "Find and replace all occurrences of find string in table", description: "Find and replace contents with capture group using regular expression",
example: "'my_library.rb' | str replace -r '(.+).rb' '$1.nu'",
result: Some(Value::test_string("my_library.nu")),
},
Example {
description: "Find and replace all occurrences of find string using regular expression",
example: "'abc abc abc' | str replace -ar 'b' 'z'",
result: Some(Value::test_string("azc azc azc")),
},
Example {
description: "Find and replace all occurrences of find string in table using regular expression",
example: example:
"[[ColA ColB ColC]; [abc abc ads]] | str replace -a 'b' 'z' ColA ColC", "[[ColA ColB ColC]; [abc abc ads]] | str replace -ar 'b' 'z' ColA ColC",
result: Some(Value::List { result: Some(Value::List {
vals: vec![Value::Record { vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()], cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
@ -136,9 +163,9 @@ impl Command for SubCommand {
}), }),
}, },
Example { Example {
description: "Find and replace all occurrences of find string in record", description: "Find and replace all occurrences of find string in record using regular expression",
example: example:
"{ KeyA: abc, KeyB: abc, KeyC: ads } | str replace -a 'b' 'z' KeyA KeyC", "{ KeyA: abc, KeyB: abc, KeyC: ads } | str replace -ar 'b' 'z' KeyA KeyC",
result: Some(Value::Record { result: Some(Value::Record {
cols: vec!["KeyA".to_string(), "KeyB".to_string(), "KeyC".to_string()], cols: vec!["KeyA".to_string(), "KeyB".to_string(), "KeyC".to_string()],
vals: vec![ vals: vec![
@ -151,36 +178,26 @@ impl Command for SubCommand {
}, },
Example { Example {
description: "Find and replace contents without using the replace parameter as a regular expression", description: "Find and replace contents without using the replace parameter as a regular expression",
example: r"'dogs_$1_cats' | str replace '\$1' '$2' -n", example: r"'dogs_$1_cats' | str replace -r '\$1' '$2' -n",
result: Some(Value::test_string("dogs_$2_cats")), result: Some(Value::test_string("dogs_$2_cats")),
}, },
Example { Example {
description: "Find and replace the first occurrence using string replacement *not* regular expressions", description: "Use captures to manipulate the input text using regular expression",
example: r"'c:\some\cool\path' | str replace 'c:\some\cool' '~' -s", example: r#""abc-def" | str replace -r "(.+)-(.+)" "${2}_${1}""#,
result: Some(Value::test_string("~\\path")),
},
Example {
description: "Find and replace all occurrences using string replacement *not* regular expressions",
example: r#"'abc abc abc' | str replace -a 'b' 'z' -s"#,
result: Some(Value::test_string("azc azc azc")),
},
Example {
description: "Use captures to manipulate the input text",
example: r#""abc-def" | str replace "(.+)-(.+)" "${2}_${1}""#,
result: Some(Value::test_string("def_abc")), result: Some(Value::test_string("def_abc")),
}, },
Example { Example {
description: "Find and replace with fancy-regex", description: "Find and replace with fancy-regex using regular expression",
example: r"'a successful b' | str replace '\b([sS])uc(?:cs|s?)e(ed(?:ed|ing|s?)|ss(?:es|ful(?:ly)?|i(?:ons?|ve(?:ly)?)|ors?)?)\b' '${1}ucce$2'", example: r"'a successful b' | str replace -r '\b([sS])uc(?:cs|s?)e(ed(?:ed|ing|s?)|ss(?:es|ful(?:ly)?|i(?:ons?|ve(?:ly)?)|ors?)?)\b' '${1}ucce$2'",
result: Some(Value::test_string("a successful b")), result: Some(Value::test_string("a successful b")),
}, },
Example { Example {
description: "Find and replace with fancy-regex", description: "Find and replace with fancy-regex using regular expression",
example: r#"'GHIKK-9+*' | str replace '[*[:xdigit:]+]' 'z'"#, example: r#"'GHIKK-9+*' | str replace -r '[*[:xdigit:]+]' 'z'"#,
result: Some(Value::test_string("GHIKK-z+*")), result: Some(Value::test_string("GHIKK-z+*")),
}, },
Example { Example {
description: "Find and replace on individual lines (multiline)", description: "Find and replace on individual lines using multiline regular expression",
example: r#""non-matching line\n123. one line\n124. another line\n" | str replace -am '^[0-9]+\. ' ''"#, example: r#""non-matching line\n123. one line\n124. another line\n" | str replace -am '^[0-9]+\. ' ''"#,
result: Some(Value::test_string("non-matching line\none line\nanother line\n")), result: Some(Value::test_string("non-matching line\none line\nanother line\n")),
}, },

View file

@ -203,7 +203,7 @@ fn regex_error_in_pattern() {
cwd: dirs.test(), pipeline( cwd: dirs.test(), pipeline(
r#" r#"
'source string' 'source string'
| str replace 'source \Ufoo' "destination" | str replace -r 'source \Ufoo' "destination"
"# "#
)); ));

View file

@ -20,7 +20,7 @@ fn can_get_help(#[case] exp_result: &str) -> TestResult {
--f2:string, # f2 named no default --f2:string, # f2 named no default
--f3:int=33 # f3 named default 3 --f3:int=33 # f3 named default 3
] {{ true }}; ] {{ true }};
help t | ansi strip | find `{exp_result}` | get 0 | str replace -a '^(.*({exp_result}).*)$' '$2'"#, help t | ansi strip | find `{exp_result}` | get 0 | str replace -ar '^(.*({exp_result}).*)$' '$2'"#,
), ),
exp_result, exp_result,
) )