{"id":21194,"date":"2024-02-28T09:17:55","date_gmt":"2024-02-28T14:17:55","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=21194"},"modified":"2024-02-28T09:17:55","modified_gmt":"2024-02-28T14:17:55","slug":"rust-regex-crate","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/rust-regex-crate\/","title":{"rendered":"A guide to Regex Crate"},"content":{"rendered":"<p><a href=\"https:\/\/qxf2.com\/?utm_source=rust_regex_crate&#038;utm_medium=click&#038;utm_campaign=From%20blog'\" rel=\"noopener\" target=\"_blank\">Qxf2<\/a> is exploring commonly used Rust crates. We are writing a series of posts on useful crates to help you to understand how to use them with examples. This is blog is about the Rust Regex Crate.<\/p>\n<p><strong>Disclaimer:<\/strong> We are not developers. But we make it a point to share out learning. This post was worked on in late 2023. Depending on when you are reading this, standards for writing good Rust programs might have evolved.<\/p>\n<hr>\n<h4>Introduction to regex crate:<\/h4>\n<p>The <a href=\"https:\/\/docs.rs\/regex\/latest\/regex\/\" rel=\"noopener\" target=\"_blank\">Rust Regex Crate<\/a> is a powerful tool for working with regular expressions. It helps you search, match, and manipulate text patterns with ease. Regex Crate is used for validating user input or extracting data from strings. The Regex Crate simplifies the complex pattern-matching tasks in your code.<br \/>\nIn this post we are providing the following Regex Crate method details with useful example that can be referred to understand the Regex Crate module better.<\/p>\n<ul>\n<li>Regex::find<\/li>\n<li>Regex::find_iter<\/li>\n<li>Regex::replace<\/li>\n<li>Regex::splitn<\/li>\n<\/ul>\n<hr>\n<h4>Prerequisite:<\/h4>\n<p>To include this Crate to your project, add &#8220;regex&#8221; to your &#8220;Cargo.toml&#8221; file or use the command <\/p>\n<pre lang=\"rust\">cargo add regex<\/pre>\n<p>Refer <strong><code><a href=\"https:\/\/doc.rust-lang.org\/cargo\/reference\/manifest.html\" rel=\"noopener\" target=\"_blank\">Cargo.toml<\/a><\/code><\/strong> file<\/p>\n<pre lang=\"rust\">\r\n[package]\r\nname = \"rust-motw-regex\"\r\nversion = \"0.1.0\"\r\nedition = \"2021\"\r\n\r\n# See more keys and their definitions at https:\/\/doc.rust-lang.org\/cargo\/reference\/manifest.html\r\n[dependencies]\r\nregex = \"1.10.2\"\r\nansi_term = \"0.12.1\"\r\n<\/pre>\n<p><strong>Note:<\/strong> Used ansi_term crate is a library for colours and formatting.<\/p>\n<hr>\n<p>Since our regex project is going to have  multiple binaries we need to create an <code>src\/bin<\/code> directory, where we will place our executable. Command to run the individual example from the terminal from project directory.<\/p>\n<pre lang=\"rust\">cargo run --bin <example filename> <\/pre>\n<p><strong>Note:<\/strong> replace example filename with the actual name.<\/p>\n<hr>\n<h4>Regex::find:<\/h4>\n<p>The <a href=\"https:\/\/docs.rs\/regex\/latest\/regex\/struct.Regex.html#method.find\" rel=\"noopener\" target=\"_blank\">find()<\/a> method in the Regex Crate actively searches for the first occurrence of a pattern in a text string. It returns a result that allows you to check if a match was found and obtain details about its position. This method is handy for pinpointing specific content in your text using regular expressions with simplicity and precision.<\/p>\n<h6>Refer the below example:<\/h6>\n<pre lang=\"Rust\">\r\n\/* find(): searches for the first occurrence of a regex pattern in a string.\r\nThe below takes input sentence and search word from the user and prints the \r\nposition for the search word.\r\nTo handle case insensitive used RegexBuilder\r\n*\/\r\nuse regex::RegexBuilder;\r\nuse std::io;\r\nuse ansi_term::Colour::{Red, Green};\r\n\r\nfn find_word_in_sentence(sentence: &str, search_word: &str) -> Result<String, regex::Error> {\r\n    \/\/ Create a Regex object for the pattern\r\n    let mut binding = RegexBuilder::new(search_word);\r\n    let regex_builder = binding.case_insensitive(true);\r\n\r\n    \/\/ Check if the regex compilation was successful\r\n    let re = match regex_builder.build() {\r\n        Ok(re) => re,\r\n        Err(err) => {\r\n            eprintln!(\"Error: {}\", err);\r\n            return Err(err);\r\n        }\r\n    };\r\n    \r\n    \/\/ Search for the pattern in the sentence\r\n    if let Some(mat) = re.find(sentence) {\r\n        let matched_text = mat.as_str();\r\n        let start = mat.start();\r\n        let end = mat.end();\r\n\r\n        let word_position = format!(\"Found '{}' at positions {}-{}\", matched_text, start, end);\r\n        Ok(word_position)\r\n    } else {\r\n        let word_position = \"Word not found in the provided sentence. \".to_string();\r\n        Ok(word_position)\r\n    }\r\n}\r\n\r\nfn prompt_user_input(prompt: &str) -> String {\r\n    println!(\"{}\", prompt);\r\n    let mut input = String::new();\r\n    match io::stdin().read_line(&mut input) {\r\n        Ok(_) => input.trim().to_string(),\r\n        Err(error) => {\r\n            eprintln!(\"Failed to read line: {}\", error);\r\n            std::process::exit(1);\r\n        }\r\n    }\r\n}\r\n\/\/ Program starting point\r\nfn main() {\r\n    let sentence = prompt_user_input(\"Input Sentence:\");\r\n    println!(\"Sentence: {}\", sentence);\r\n\r\n    let search_word = prompt_user_input(\"Enter Search word from the input sentence:\");\r\n    println!(\"Search Word: {}\", search_word);\r\n\r\n    \/\/ Validate input\r\n    if sentence.is_empty() || search_word.is_empty() {\r\n        println!(\"{}\",Red.paint(\"Error: Input cannot be empty.\"));\r\n        return;\r\n    }\r\n    match find_word_in_sentence(&sentence, &search_word) {\r\n        Ok(word_position) => {\r\n            println!(\"{}\", Green.paint(word_position));\r\n        }\r\n        Err(err) => println!(\"Error: {}\", err),\r\n    }\r\n}\r\n<\/pre>\n<h6>Check the output of the above code:<\/h6>\n<p><img decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/02\/find_word-2.jpg\" alt=\"Regex_find\" \/><\/p>\n<hr>\n<h4>Regex::find_iter:<\/h4>\n<p>The <a href=\"https:\/\/docs.rs\/regex\/latest\/regex\/struct.Regex.html#method.find_iter\" rel=\"noopener\" target=\"_blank\">find_iter<\/a>() method in the Regex Crate actively searches for all occurrences of a pattern in a text string. It returns an iterator that enables you to iterate over each match and retrieve details about their positions. This method is useful for handling multiple matches within your text using regular expressions with ease and efficiency.<\/p>\n<h6>Refer the below example:<\/h6>\n<pre lang=\"rust\">\r\n\/*\r\nThe below code uses find_iter() method.\r\nIn the provided text the script searches for email address pattern\r\n*\/\r\n\r\nuse regex::Regex;\r\nuse std::error::Error;\r\nuse ansi_term::Colour::{Green};\r\n\r\nfn search_email_pattern_from_sentence(text: &str) -> Result<Vec<String>, Box<dyn Error>> {\r\n    let mut emails = Vec::new(); \/\/ Store the found email addresses\r\n\r\n    \/\/ Define a valid regex pattern for email address\r\n    let pattern = r\"\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}\\b\";\r\n\r\n    \/\/ Create a Regex object\r\n      let re = match Regex::new(pattern) {\r\n        Ok(re) => re,\r\n        Err(err) => return Err(err.into()), \/\/ Convert the regex error to Box<dyn Error>\r\n    };\r\n\r\n    \/\/ Find all matches in the text\r\n    for mat in re.find_iter(text) {\r\n        emails.push(mat.as_str().to_string()); \/\/ Store the found email addresses\r\n    }\r\n\r\n    if emails.is_empty() {\r\n        return Err(\"No email addresses found in the text.\".into()); \/\/ Return an error if no emails are found\r\n    }\r\n\r\n    Ok(emails) \/\/ Return the found email addresses\r\n}\r\n\r\nfn main() {\r\n    let text = \"Contact us at mak@qxf2.com or support@qxf2.com or invalid.com for assistance.\";\r\n    println!(\"The text: {} \", text);\r\n\r\n    match search_email_pattern_from_sentence(text) {\r\n        Ok(emails) => {\r\n            \/\/ Print the matches\r\n            println!(\"====================Email from text=============================\");\r\n            for email in emails {\r\n                println!(\"{}\", Green.paint(email));\r\n            }\r\n        }\r\n        Err(err) => {\r\n            println!(\"{}\", err);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h6>Check the output of the above code:<\/h6>\n<p><img decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/02\/find_iter.jpg\" alt=\"Regex_find_iter\" \/><\/p>\n<hr>\n<h4>Regex::replace_all<\/h4>\n<p>The <a href=\"https:\/\/docs.rs\/regex\/latest\/regex\/struct.Regex.html#method.replace_all\" rel=\"noopener\" target=\"_blank\">replace_all<\/a>() method in the Regex Crate actively replaces all occurrences of a pattern in a text string with a specified replacement. It provides a convenient way to globally modify your text based on regular expression matches, ensuring that every instance of the pattern is replaced throughout the entire string.<\/p>\n<h6>Refer the below example:<\/h6>\n<pre lang=\"rust\">\r\n\/*\r\nThe below code uses Regex:replace_all() method.\r\nThe program prompts user to enter sentence, then word to replace and replacement word\r\n *\/\r\nuse regex::Regex;\r\nuse std::io;\r\nuse ansi_term::Colour::{Red, Blue, Green, Yellow};\r\n\r\nfn prompt_user_input(prompt: &str) -> String {\r\n    println!(\"{}\", prompt);\r\n    let mut input = String::new();\r\n    match io::stdin().read_line(&mut input) {\r\n        Ok(_) => input.trim().to_string(),\r\n        Err(error) => {\r\n            eprintln!(\"Failed to read line: {}\", error);\r\n            std::process::exit(1);\r\n        }\r\n    }\r\n}\r\n\r\nfn replace_text(sentence: &str, word_to_replace: &str, replacement_word: &str) -> Result<String, regex::Error> {\r\n    \/\/ Create a Regex object for the word to be replaced\r\n    let re = match Regex::new(&format!(r\"\\b{}\\b\", regex::escape(word_to_replace))) {\r\n        Ok(re) => re,\r\n        Err(err) => return Err(err),\r\n    };\r\n\r\n    \/\/ Replace all occurrences of the word\r\n    let replaced_sentence = re.replace_all(sentence, replacement_word).to_string();\r\n    Ok(replaced_sentence)\r\n}\r\n\r\n\/\/ Program starting point\r\nfn main() {\r\n    let sentence = prompt_user_input(\"Enter a Sentence:\");\r\n    println!(\"Sentence: {}\", Blue.bold().paint(&sentence));\r\n\r\n    let word_to_replace = prompt_user_input(\"Enter the word to replace:\");\r\n    println!(\"word to replace: {}\", Red.bold().paint(&word_to_replace));\r\n\r\n    let replacement_word = prompt_user_input(\"Enter the replacement word:\");\r\n    println!(\"Enter replacement word: {}\", Green.bold().paint(&replacement_word));\r\n    \r\n    match replace_text(&sentence, &word_to_replace, &replacement_word) {\r\n        Ok(replaced_sentence) => {\r\n            println!(\"Modified sentence: {}\", Yellow.bold().paint(&replaced_sentence));\r\n        }\r\n        Err(err) => eprintln!(\"Error replacing text: {}\", err),\r\n    }\r\n}\r\n<\/pre>\n<h6>Check the output of the above code:<\/h6>\n<p><img decoding=\"async\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/02\/regex_replace_all-1.jpg\" alt=\"Regex_replace_all\" \/><\/p>\n<hr>\n<h4>Regex::splitn<\/h4>\n<p>The <a href=\"https:\/\/docs.rs\/regex\/latest\/regex\/struct.Regex.html#method.split\" rel=\"noopener\" target=\"_blank\">splitn<\/a>() method in the Regex Crate actively divides a text string into parts based on a specified pattern. It creates an iterator that allows you to loop through the separated segments. This method is useful for breaking down a string into meaningful components using regular expressions, providing flexibility in handling different parts of the text.<\/p>\n<h6>Refer the below example:<\/h6>\n<pre lang=\"rust\">\r\n\/* \r\nThe below example uses splitn method.\r\nThe below example uses splitn to extract key details like product name, price, and description.\r\n*\/ \r\nuse regex::Regex;\r\nuse std::error::Error; \r\nuse ansi_term::Colour::{Red, Blue};\r\n \r\nfn split_text(product_description: &str) -> Result<Vec<&#038;str>, Box<dyn Error>> {\r\n    let pattern = r\"\\|\"; \/\/ Assuming \"|\" is the delimiter\r\n    let re = match Regex::new(pattern) {\r\n        Ok(re) => re,\r\n        Err(e) => return Err(Box::new(e)), \r\n    };\r\n \r\n        let parts: Vec<&#038;str> = re.splitn(product_description, 3).collect();\r\n        Ok(parts)\r\n    }\r\n\r\nfn main() {\r\n    let product_description = \"Widget | $29.99 | A high-quality widget for your needs.\";\r\n    println!(\"The Product Description: {}\", Red.paint(product_description));\r\n    println!(\"{}\", Blue.paint(\"================The Split:Product\/Price\/Desc===================================\"));\r\n    match split_text(&product_description) {\r\n        Ok(parts) => {\r\n            if parts.len() >= 3 {\r\n                let product_name = parts[0].trim();\r\n                let price = parts[1].trim();\r\n                let description = parts[2].trim();\r\n\r\n                println!(\"Product Name: {}\", product_name);\r\n                println!(\"Price: {}\", price);\r\n                println!(\"Description: {}\", description);\r\n            } else {\r\n                println!(\"Invalid product description format\");\r\n            }\r\n        }\r\n        Err(err) => {\r\n            println!(\"Failed to split product description: {}\", err);\r\n            \/\/ You can take further actions here for error handling if needed.\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h6>Check the output of the above code:<\/h6>\n<p><img src=https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2024\/02\/Regex_split.jpg alt=\"Regex_splitn\" \/><\/p>\n<hr>\n<p>Hope the above provided Regex Crate methods with examples are useful for the understanding of the Regex crate. We explored essential methods like find(), find_iter(), replace_all(), and splitn(), each offering unique capabilities. Whether you&#8217;re locating patterns, iterating through matches, globally replacing text, or segmenting strings, these methods simplify complex tasks.<br \/>\nIn our next upcoming blog we are coming up with few more methods of Regex Crate with examples.<\/p>\n<hr>\n<h4>Hire technical testers from Qxf2<\/h4>\n<p>Qxf2 hires technical testers who are ahead of the curve. We go out of our way to stay in touch with early market trends. As you can see from this post, we have transitioned to learning, using and sharing Rust code well before Rust has become mainstream. If you are looking for highly technical engineers who excel at testing, please get in <a href=\"https:\/\/qxf2.com\/contact?utm_source=rust_regex_crate&#038;utm_medium=click&#038;utm_campaign=From%20blog'\" rel=\"noopener\" target=\"_blank\">touch with us<\/a>.<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>Qxf2 is exploring commonly used Rust crates. We are writing a series of posts on useful crates to help you to understand how to use them with examples. This is blog is about the Rust Regex Crate. Disclaimer: We are not developers. But we make it a point to share out learning. This post was worked on in late 2023. [&hellip;]<\/p>\n","protected":false},"author":37,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[306],"tags":[],"class_list":["post-21194","post","type-post","status-publish","format-standard","hentry","category-rust"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/21194","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/37"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=21194"}],"version-history":[{"count":61,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/21194\/revisions"}],"predecessor-version":[{"id":21550,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/21194\/revisions\/21550"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=21194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=21194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=21194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}