^[Regex &Rust]$
Привет друзья 👋, I hope everyone is doing good, this is kind of personal blog and I may be wrong at few places, so if you find anything wrong feel free to let me know.
-A Regex amateur
So, recently I was just browsing YouTube for some good programming related videos, so uh suddenly I came along a video titled “Regular Expressions — Enough to be dangerous” by Engineer man. To be frank I had an idea that regular expressions are quite tough so never tried 😅, but anyways this was my first attempt of learning regular expressions and honestly I got to accept the fact that this video makes understanding the basics of regex extremely easy. Along with this small video there’s a small website which also helps one to learn called as debuggex.
So, kind of easy I would say it was to learn the basics of regex, some of the basics I would like to highlight over here :
Target String : "Regex"Regex : ^[a-zA-Z$]{5}$
Here ^ denotes the starting , [] denotes the range and {} denotes the number of times and $ denotes the end of string, therefore at the starting we have either any of the alphabets from A- Z either caps of normal, and yes “R” definitely matches it rest of the alphabets “egex” also belong to the same range starting from a to z might be in caps or not in caps, and finally the end of the string also has one of the alphabet which falls into this category, and we need to run this check 5 times because the size of the string equals to five, and finally we completed our first basic regular expression.
Now , after some more tinkering I would say I was enjoying it, then a small idea popped into mind why not try this with a programming language as the site where I was practicing my regex would only support Python, Javascript . So I thought of applying regex using one of the programming languages I love that is Rust.
After a small google search I came up with a small crate known as regex which would help me do this, the very first step of using any random crate in Rust is to add it up into the dependencies at the Cargo.toml file at Rust. Later exploring this crate I finally tried out a small example to match a simple phone number using the regex crate 😸.
use regex::Regex;fn main() {
let p_number = Regex::new(r"^[0-9]{4}-[0-9]{3}-[0-9]{3}$").unwrap();assert!(p_number.is_match("7891-661-690")); //Checking a phone //numberprintln!("Matched {}", "😀");
Voila, if you compile and run this it will print “Matched 😀” if you are not familiar with rust let me explain this to you, the pattern or the regular expression is first initialized into the “p_number” variable, and then .unwrap() macro is used which returns the contained [Ok
] value, consuming the self
value. If you do not know much about it you can check it out here.
Then the assert!() macro which asserts that a boolean expression is true
at runtime.This assert!() will invoke the panic!
macro if the provided expression cannot be evaluated to true
at runtime, along with is_match function which returns true if and only if there is a match for the regex in the string given. Now finally it prints Matched 😃 if the code evaluates to true and panics if evaluates to false. We will see into it using some examples.
Now, let us check a simple email, along with our regex in rust.
let email: Regex = Regex::new(r"^[a-zA-z0-9]+@+[a-zA-Z0-9]+\.[a-z]{3}$").unwrap();assert!(email.is_match("test123@test123.com")); //Checking an email
This simple regex using the regex crate checks if the email we entered or declared is correct or not. Now we will check both of the regex using debuggex and also compile and run our code, we will also intentionally enter the wrong value so the program panics and we can understand how actually the assert! function works.
Yes 😄, both of our regular expressions are correct and they match they target strings.
Now, let us look into our code:
Woop! It finally worked and ran! Now let us alter the target string and see if the assert function actually invokes panic if the expression is false!
Well Yes, as expected our program panics, if the target string does not match the regular expression! 😄
Therefore, this was a small introduction of using regular expressions with Rust, I hope you will play around the crate . Till then have fun and happy learning.
You can check out rest of the code here, and feel free to use it while learning, I would be happy if my small snap of code, helps someone to learn and have fun 😄.
Goodbye 👋!