Day 9

A Rust Regex Pattern Matcher

Unfortunately, you can't run this in the browser here

This program uses the regex crate and reads from stdin, so it must be run locally — it cannot run in the browser.

Add regex = "1" to your Cargo.toml dependencies, then run cargo run.


Description


This is an interactive Rust program that lets you enter a regex pattern and a string to test it against. It uses the regex crate to compile the pattern and prints whether the text matches, then asks if you want to try another — looping until you say no.

View the source code


use regex::Regex;
use std::io;
fn main() {
 loop {
  let mut pattern_input = String::new();
  println!("Enter a regex pattern:");
  io::stdin().read_line(&mut pattern_input).expect("Failed to read line");
  let pattern = pattern_input.trim();
  let re = Regex::new(pattern).unwrap();
  let mut text_input = String::new();
  println!("Enter text to match against the pattern:");
  io::stdin().read_line(&mut text_input).expect("Failed to read line");
  let text = text_input.trim();
  println!("{}", re.is_match(text));
  println!("Do you want to try another pattern? (y/n)");
  let mut continue_input = String::new();
  io::stdin().read_line(&mut continue_input).expect("Failed to read line");
  if continue_input.trim().to_lowercase() != "y" {
   break;
  }
 }
}

Previous Day
Next Day