Let's start with the answer of karthik manchala and Shepmaster :
put all the lines in an array and iterate over the array. If your application logic "replaces all A with B, then all C with D, then all E with F", then the code will reflect this repeating logic.
Instead of storing strings in an array, I would recommend storing compiled regular expressions there, so as not to rebuild them every time.
Here is the code:
extern crate regex; use regex::Regex; use std::env::args; use std::iter::FromIterator; fn main() { let patterns = [("your", "mine"), ("you are", "I am")]; let patterns = Vec::from_iter(patterns.into_iter().map(|&(k, v)| { (Regex::new(k).expect(&format!("Can't compile the regular expression: {}", k)), v) })); for arg in args().skip(1) { println!("Argument: {}", arg); for &(ref re, replacement) in patterns.iter() { let got = re.replace_all(&arg, replacement); if got != arg { println!("Changed to: {}", got); continue; } } } }
That would be so, but for completeness, I would like to add that if you want superior performance, you can use the MARK function, which is present in the PCRE regular expression engine ( pcre ).
With MARK and such patterns
"(?x) ^ (?: (*MARK:0) first pattern \ | (*MARK:1) second pattern \ | (*MARK:2) third pattern \ )"
you can use the MARK number for classification, or in your case as an index in an array with replacements. This is often better than using multiple regexes because the subject line is processed only once.
source share