During my self-taught programming journey, one of the programming tools and concepts that intimidated me and even new programmers was regular expressions and understanding its odd syntax right away and need to check and write it out.
The photo below was how I was feeling my first time trying to decipher regex syntax on my own.
Why use it?
The purpose of regular expressions is to find strings that match a pattern, which would be the regular expression and compare them with string input. I eventually understood the use cases for regex I have seen, especially with using it to check for certain characters in passwords, emails for example and check for input validation. I began to feel more comfortable with its uses
Code example
lets say we have an input field on a signup form on an app or website where we ask for someones name during signup and we want it to make sure the input contains letters and not numbers.
In Javascript you can use the RegExp Constructor or as literal string encased in forward slashes which I used below and is used in most languages.
let re = /^([a-z])/i;
it can be read as open literal string /^firstletter(capturing group #1 [Character set range])/ close literal string /flag(s)
a capturing group checks the condition for a substring position which in this example is the first letter. A character set range lets you define a range to check for and set the criteria for your expression. The i flag means string isn't case sensitive.
this expression would then be tested by the input using the test function which will check take the input as a parameter and return a Boolean value true or false based on if the condition is match for the first name.
//captialize first name if lowercase.
const isValid = re.test(firstname.charAt(0).toUpperCase());
console.log(isValid);
//pseudocode
//if false return error message.
//if true, let user signup
Why bother with Regex?
Once you learn it and implement it in one language, you can apply it to other languages.
Best practices and simplifying code
Once you reach a comfort zone with regular expressions and spend time with it. You will see that Regex can be used to turn multiple lines of long switch statements of strings into one line of code.
How to get Better?
What helped me with getting better at Regex, aside from watching videos and reading articles, is hands on code practice and finding good resources that helped with understanding. For practice, one resource I used was Hackerrank. It has a section dedicated to Regex on the home page from simple to complex to try while you use and refer to a reference sheet. Also the 10 days of JavaScript tutorial is perfect for beginners. Try to find some code you written and see if you can apply regex to it. Also take it in strides and go at your own pace.
Testing
One website that is good for checking and testing your regex pattern with custom input is regex101.com. I came across it when I was first learning about regex and is a good resource.
If anyone has any suggestions, thoughts, opinions, or want to add anything else. Feel free to comment.
Until next time, happy learning and coding. I left some links and references below as well.
Resources and further reading:
https://blog.bitsrc.io/4-practical-use-cases-for-regular-expressions-b6ab140894fd
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285
http://www.websiterepairguy.com/articles/re/12_re.html
https://regex101.com
https://www.computerhope.com/jargon/r/regex.htm
https://www.hackerrank.com/dashboard (10 days of JavaScript and Regex)
Top comments (0)