1use fancy_regex::{NoExpand, Regex};
2use nu_cmd_base::input_handler::{operate, CmdArgument};
3use nu_engine::command_prelude::*;
4
5struct Arguments {
6 all: bool,
7 find: Spanned<String>,
8 replace: Spanned<String>,
9 cell_paths: Option<Vec<CellPath>>,
10 literal_replace: bool,
11 no_regex: bool,
12 multiline: bool,
13}
14
15impl CmdArgument for Arguments {
16 fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
17 self.cell_paths.take()
18 }
19}
20
21#[derive(Clone)]
22pub struct StrReplace;
23
24impl Command for StrReplace {
25 fn name(&self) -> &str {
26 "str replace"
27 }
28
29 fn signature(&self) -> Signature {
30 Signature::build("str replace")
31 .input_output_types(vec![
32 (Type::String, Type::String),
33 (Type::table(), Type::table()),
35 (Type::record(), Type::record()),
36 (
37 Type::List(Box::new(Type::String)),
38 Type::List(Box::new(Type::String)),
39 ),
40 ])
41 .required("find", SyntaxShape::String, "The pattern to find.")
42 .required("replace", SyntaxShape::String, "The replacement string.")
43 .rest(
44 "rest",
45 SyntaxShape::CellPath,
46 "For a data structure input, operate on strings at the given cell paths.",
47 )
48 .switch("all", "replace all occurrences of the pattern", Some('a'))
49 .switch(
50 "no-expand",
51 "do not expand capture groups (like $name) in the replacement string",
52 Some('n'),
53 )
54 .switch(
55 "regex",
56 "match the pattern as a regular expression in the input, instead of a substring",
57 Some('r'),
58 )
59 .switch(
60 "multiline",
61 "multi-line regex mode (implies --regex): ^ and $ match begin/end of line; equivalent to (?m)",
62 Some('m'),
63 )
64 .allow_variants_without_examples(true)
65 .category(Category::Strings)
66 }
67
68 fn description(&self) -> &str {
69 "Find and replace text."
70 }
71
72 fn search_terms(&self) -> Vec<&str> {
73 vec!["search", "shift", "switch", "regex"]
74 }
75
76 fn is_const(&self) -> bool {
77 true
78 }
79
80 fn run(
81 &self,
82 engine_state: &EngineState,
83 stack: &mut Stack,
84 call: &Call,
85 input: PipelineData,
86 ) -> Result<PipelineData, ShellError> {
87 let find: Spanned<String> = call.req(engine_state, stack, 0)?;
88 let replace: Spanned<String> = call.req(engine_state, stack, 1)?;
89 let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 2)?;
90 let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
91 let literal_replace = call.has_flag(engine_state, stack, "no-expand")?;
92 let no_regex = !call.has_flag(engine_state, stack, "regex")?
93 && !call.has_flag(engine_state, stack, "multiline")?;
94 let multiline = call.has_flag(engine_state, stack, "multiline")?;
95
96 let args = Arguments {
97 all: call.has_flag(engine_state, stack, "all")?,
98 find,
99 replace,
100 cell_paths,
101 literal_replace,
102 no_regex,
103 multiline,
104 };
105 operate(action, args, input, call.head, engine_state.signals())
106 }
107
108 fn run_const(
109 &self,
110 working_set: &StateWorkingSet,
111 call: &Call,
112 input: PipelineData,
113 ) -> Result<PipelineData, ShellError> {
114 let find: Spanned<String> = call.req_const(working_set, 0)?;
115 let replace: Spanned<String> = call.req_const(working_set, 1)?;
116 let cell_paths: Vec<CellPath> = call.rest_const(working_set, 2)?;
117 let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
118 let literal_replace = call.has_flag_const(working_set, "no-expand")?;
119 let no_regex = !call.has_flag_const(working_set, "regex")?
120 && !call.has_flag_const(working_set, "multiline")?;
121 let multiline = call.has_flag_const(working_set, "multiline")?;
122
123 let args = Arguments {
124 all: call.has_flag_const(working_set, "all")?,
125 find,
126 replace,
127 cell_paths,
128 literal_replace,
129 no_regex,
130 multiline,
131 };
132 operate(
133 action,
134 args,
135 input,
136 call.head,
137 working_set.permanent().signals(),
138 )
139 }
140
141 fn examples(&self) -> Vec<Example> {
142 vec![
143 Example {
144 description: "Find and replace the first occurrence of a substring",
145 example: r"'c:\some\cool\path' | str replace 'c:\some\cool' '~'",
146 result: Some(Value::test_string("~\\path")),
147 },
148 Example {
149 description: "Find and replace all occurrences of a substring",
150 example: r#"'abc abc abc' | str replace --all 'b' 'z'"#,
151 result: Some(Value::test_string("azc azc azc")),
152 },
153 Example {
154 description: "Find and replace contents with capture group using regular expression",
155 example: "'my_library.rb' | str replace -r '(.+).rb' '$1.nu'",
156 result: Some(Value::test_string("my_library.nu")),
157 },
158 Example {
159 description: "Find and replace contents with capture group using regular expression, with escapes",
160 example: "'hello=world' | str replace -r '\\$?(?<varname>.*)=(?<value>.*)' '$$$varname = $value'",
161 result: Some(Value::test_string("$hello = world")),
162 },
163 Example {
164 description: "Find and replace all occurrences of found string using regular expression",
165 example: "'abc abc abc' | str replace --all --regex 'b' 'z'",
166 result: Some(Value::test_string("azc azc azc")),
167 },
168 Example {
169 description: "Find and replace all occurrences of found string in table using regular expression",
170 example:
171 "[[ColA ColB ColC]; [abc abc ads]] | str replace --all --regex 'b' 'z' ColA ColC",
172 result: Some(Value::test_list (
173 vec![Value::test_record(record! {
174 "ColA" => Value::test_string("azc"),
175 "ColB" => Value::test_string("abc"),
176 "ColC" => Value::test_string("ads"),
177 })],
178 )),
179 },
180 Example {
181 description: "Find and replace all occurrences of found string in record using regular expression",
182 example:
183 "{ KeyA: abc, KeyB: abc, KeyC: ads } | str replace --all --regex 'b' 'z' KeyA KeyC",
184 result: Some(Value::test_record(record! {
185 "KeyA" => Value::test_string("azc"),
186 "KeyB" => Value::test_string("abc"),
187 "KeyC" => Value::test_string("ads"),
188 })),
189 },
190 Example {
191 description: "Find and replace contents without using the replace parameter as a regular expression",
192 example: r"'dogs_$1_cats' | str replace -r '\$1' '$2' -n",
193 result: Some(Value::test_string("dogs_$2_cats")),
194 },
195 Example {
196 description: "Use captures to manipulate the input text using regular expression",
197 example: r#""abc-def" | str replace -r "(.+)-(.+)" "${2}_${1}""#,
198 result: Some(Value::test_string("def_abc")),
199 },
200 Example {
201 description: "Find and replace with fancy-regex using regular expression",
202 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'",
203 result: Some(Value::test_string("a successful b")),
204 },
205 Example {
206 description: "Find and replace with fancy-regex using regular expression",
207 example: r#"'GHIKK-9+*' | str replace -r '[*[:xdigit:]+]' 'z'"#,
208 result: Some(Value::test_string("GHIKK-z+*")),
209 },
210 Example {
211 description: "Find and replace on individual lines using multiline regular expression",
212 example: r#""non-matching line\n123. one line\n124. another line\n" | str replace --all --multiline '^[0-9]+\. ' ''"#,
213 result: Some(Value::test_string("non-matching line\none line\nanother line\n")),
214 },
215
216 ]
217 }
218}
219
220struct FindReplace<'a>(&'a str, &'a str);
221
222fn action(
223 input: &Value,
224 Arguments {
225 find,
226 replace,
227 all,
228 literal_replace,
229 no_regex,
230 multiline,
231 ..
232 }: &Arguments,
233 head: Span,
234) -> Value {
235 match input {
236 Value::String { val, .. } => {
237 let FindReplace(find_str, replace_str) = FindReplace(&find.item, &replace.item);
238 if *no_regex {
239 if *all {
241 Value::string(val.replace(find_str, replace_str), head)
242 } else {
243 Value::string(val.replacen(find_str, replace_str, 1), head)
244 }
245 } else {
246 let flags = match multiline {
248 true => "(?m)",
249 false => "",
250 };
251 let regex_string = flags.to_string() + find_str;
252 let regex = Regex::new(®ex_string);
253
254 match regex {
255 Ok(re) => {
256 if *all {
257 Value::string(
258 {
259 if *literal_replace {
260 re.replace_all(val, NoExpand(replace_str)).to_string()
261 } else {
262 re.replace_all(val, replace_str).to_string()
263 }
264 },
265 head,
266 )
267 } else {
268 Value::string(
269 {
270 if *literal_replace {
271 re.replace(val, NoExpand(replace_str)).to_string()
272 } else {
273 re.replace(val, replace_str).to_string()
274 }
275 },
276 head,
277 )
278 }
279 }
280 Err(e) => Value::error(
281 ShellError::IncorrectValue {
282 msg: format!("Regex error: {e}"),
283 val_span: find.span,
284 call_span: head,
285 },
286 find.span,
287 ),
288 }
289 }
290 }
291 Value::Error { .. } => input.clone(),
292 _ => Value::error(
293 ShellError::OnlySupportsThisInputType {
294 exp_input_type: "string".into(),
295 wrong_type: input.get_type().to_string(),
296 dst_span: head,
297 src_span: input.span(),
298 },
299 head,
300 ),
301 }
302}
303
304#[cfg(test)]
305mod tests {
306 use super::*;
307 use super::{action, Arguments, StrReplace};
308
309 fn test_spanned_string(val: &str) -> Spanned<String> {
310 Spanned {
311 item: String::from(val),
312 span: Span::test_data(),
313 }
314 }
315
316 #[test]
317 fn test_examples() {
318 use crate::test_examples;
319
320 test_examples(StrReplace {})
321 }
322
323 #[test]
324 fn can_have_capture_groups() {
325 let word = Value::test_string("Cargo.toml");
326
327 let options = Arguments {
328 find: test_spanned_string("Cargo.(.+)"),
329 replace: test_spanned_string("Carga.$1"),
330 cell_paths: None,
331 literal_replace: false,
332 all: false,
333 no_regex: false,
334 multiline: false,
335 };
336
337 let actual = action(&word, &options, Span::test_data());
338 assert_eq!(actual, Value::test_string("Carga.toml"));
339 }
340}