Regex-ified!

So what are regular expressions? and how do I use regular expressions in ruby?

Take a look at this piece of code:

/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

What is this? At first glance it looks like symbols on babylonian clay tablets to me, but what we have here is a simple regular expression that validates most email addresses.

While they may seem daunting at first, they are essential in web app development when dealing with data. Regular expressions are a powerful way to express a search pattern as a sequence of characters.

In a world with increasing amounts of collected data, it is more important that ever to have a tool at your disposal to be able to filter through the data and find the patterns you are looking for. A well crafted regular expression can replace much more complicate code sequences, and reduce the probability of bugs, in our simple example in the Professional Ruby on Rails developer course we use it to ensure the email address matches standard email address patterns.

In addition to that, regular expressions can be easily made configurable, and switched out in application code when the pattern that we need to match changes. This would be more difficult to do if we used custom logic instead of regex matching.

In Ruby as we have seen, we often use regular expressions to validate inputs to our application and to confirm that the output matches our expectations in tests.

In addition, here is a really cool tool that lets you try out your Ruby regular expressions online - http://rubular.com/ I would highly recommend playing with this tool and familiarizing yourself with regex.

Happy coding,

- Mashrur

Don't forget to sign-up for my newsletter to get the best discount coupons (better rates than udemy or anywhere else on web) for my courses since I control them, and a big one is coming up!