First commit

This commit is contained in:
Pedro de Oliveira 2023-05-26 16:20:33 +01:00
commit 94e43c1f21
4 changed files with 2883 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
/regexer.iml
/.idea/

2784
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

11
Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "regexer"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
eframe = "0.22.0"
env_logger = "0.10.0"
regex = "1.8.3"

85
src/main.rs Normal file
View File

@ -0,0 +1,85 @@
#![windows_subsystem = "windows"]
use eframe::egui;
use regex::Regex;
fn replace_with_regex(pattern: &str, replacement: &str, input: &str) -> String {
let regex = Regex::new(pattern).unwrap();
if let Some(captures) = regex.captures(input) {
let replaced = replacement
.split_whitespace()
.map(|part| {
if part.starts_with('{') && part.ends_with('}') {
let index_str = &part[1..part.len() - 1];
let index: usize = index_str.parse().unwrap();
captures.get(index).map_or(part, |m| m.as_str())
} else {
part
}
})
.collect::<Vec<&str>>()
.join(" ");
return replaced;
}
String::new()
}
fn main() -> Result<(), eframe::Error> {
env_logger::init();
let options = eframe::NativeOptions {
initial_window_size: Some(egui::vec2(300.0, 245.0)),
resizable: false,
..Default::default()
};
eframe::run_native("Regexer", options, Box::new(|_cc| Box::<MyApp>::default()))
}
struct MyApp {
pattern: String,
replacement: String,
input: String,
output: String,
}
impl Default for MyApp {
fn default() -> Self {
Self {
pattern: r"(\d+)".to_owned(),
replacement: "{1}".to_owned(),
input: "viva o benfica 123 - viva 323".to_owned(),
output: String::new(),
}
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
//ui.heading("My egui Application");
ui.horizontal(|ui| {
let pattern_label = ui.label("Pattern: ");
ui.text_edit_singleline(&mut self.pattern)
.labelled_by(pattern_label.id);
});
ui.horizontal(|ui| {
let replacement_label = ui.label("Replacement: ");
ui.text_edit_singleline(&mut self.replacement)
.labelled_by(replacement_label.id);
});
ui.vertical(|ui| {
let input_label = ui.label("Input");
ui.text_edit_multiline(&mut self.input)
.labelled_by(input_label.id);
let output_label = ui.label("Output");
ui.text_edit_multiline(&mut self.output)
.labelled_by(output_label.id);
});
if ui.button("Do it").clicked() {
self.output = replace_with_regex(&self.pattern, &self.replacement, &self.input);
}
});
}
}