• 0 Posts
  • 1 Comment
Joined 1 year ago
cake
Cake day: September 24th, 2023

help-circle
  • IMO if you’re doing something that complex you shouldn’t be using sed, but yeah you can probably do this something like:

    while read REGEX; do
      sed "$REGEX" << EOF
    your test string
    EOF
    done <list_of_regexes.txt
    

    I strongly recommend you don’t do that though. It will be absolutely full of quoting bugs. Instead write a script in a proper language to do it. I recommend Deno, or maybe even Rust.

    If you use Rust you can also use RegexSet which will be much faster (if you just want to find matches anyway). Here’s what ChatGPT made me. Not tested but it looks vaguely right.

    use regex::RegexSet;
    use std::fs::File;
    use std::io::{self, BufRead};
    use std::path::Path;
    
    fn main() -> Result<(), Box<dyn std::error::Error>> {
        // Define the fixed input string
        let input_string = "This is a test string for regex matching.";
    
        // Path to the file containing the regexes
        let regex_file_path = "regexes.txt";
    
        // Read regexes from the file
        let regexes = read_lines(regex_file_path)?
            .filter_map(Result::ok) // Filter out errors
            .collect::<Vec<String>>();
    
        // Create a RegexSet from the regexes
        let regex_set = RegexSet::new(&regexes)?;
    
        // Find the regexes that match the input string
        let matches: Vec<_> = regex_set.matches(input_string).into_iter().collect();
    
        // Print the matches
        if matches.is_empty() {
            println!("No regexes matched the input string.");
        } else {
            println!("Regexes that matched the input string:");
            for index in matches {
                println!(" - {}", regexes[index]);
            }
        }
    
        Ok(())
    }
    
    // Helper function to read lines from a file
    fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
    where
        P: AsRef<Path>,
    {
        let file = File::open(filename)?;
        Ok(io::BufReader::new(file).lines())
    }