Skip to content
Snippets Groups Projects
Commit 9d32f207 authored by Shiwin's avatar Shiwin Committed by Michael Aaron Murphy
Browse files

Implement $regex_replace (#555)

parent e4952a21
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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
......@@ -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) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment