diff --git a/examples/methods.ion b/examples/methods.ion index 00f9df4d3744fb1263b9ab8d88ccd14c4dc6b300..5c937fa6e5b00e3ee5888529a3f84ec82ad5d5d8 100644 --- a/examples/methods.ion +++ b/examples/methods.ion @@ -40,3 +40,5 @@ echo $join(array, "\n") let a = [1 2 3 4 5] let a = "1 2 3 4 5" echo $join(@split(a, " "), $join(a, " ")) + +echo $regex_replace("one two onemy anemy town", "\ o|\ a" "\ e") \ No newline at end of file diff --git a/examples/methods.out b/examples/methods.out index dfb76765f445719d9b3da9a6149aebf4fc388068..823c03d7200849df2d02899a2a0de94656d265df 100644 --- a/examples/methods.out +++ b/examples/methods.out @@ -66,3 +66,4 @@ one two three apple sauce 11 2 3 4 521 2 3 4 531 2 3 4 541 2 3 4 55 +one two enemy enemy town diff --git a/src/parser/shell_expand/words/methods/strings.rs b/src/parser/shell_expand/words/methods/strings.rs index 7f819677b99a65272fad2c9513a8b4fe0f56b794..4b7ba4432f2765c85b843a699fd2a4afd67ea7f3 100644 --- a/src/parser/shell_expand/words/methods/strings.rs +++ b/src/parser/shell_expand/words/methods/strings.rs @@ -3,6 +3,7 @@ use super::super::Select; use super::super::super::{expand_string, Expander, is_expression, slice}; use parser::assignments::is_array; use shell::plugins::methods::{self, MethodArguments, StringMethodPlugins}; +use regex::Regex; use std::path::Path; use sys; use unicode_segmentation::UnicodeSegmentation; @@ -118,6 +119,22 @@ impl<'a> StringMethod<'a> { _ => eprintln!("ion: replacen: three arguments required") } } + "regex_replace" => { + let mut args = pattern.array(); + match (args.next(),args.next()) { + (Some(replace),Some(with)) => { + match Regex::new(&replace){ + Ok(re) => { + let inp = &get_var!(); + let res = re.replace_all(&inp,&with[..]); + output.push_str(&res); + } + Err(_) => eprintln!("ion: regex_replace: error in regular expression {}",&replace) + } + } + _ => eprintln!("ion: regex_replace: two arguments required") + } + } "join" => { let pattern = pattern.join(" "); if let Some(array) = expand.array(variable, Select::All) {