For the first sight, regular expressions might look a bit confusing but after understanding the basics it is fairly easy to understand and master.
For this article, I will be using the tool called “grep” (globally search for a regular expression and print matching lines) which is a command-line utility for searching plain-text data for lines that match a given regular expression. Grep comes built-in on Unix based systems. If you are using Windows, you can download it from here: http://gnuwin32.sourceforge.net/packages/grep.htm. After getting grep we can start learning the most important things. I have created a file called “test.txt” which contains the following:
Hello World Test test is this working? i think so... random test what else should I add? tttttttttttest
With regex you can just search for a text:
grep "test" test.txt
You can match any character with writing a "."
grep ".est" test.txt
You can use it to match any number of the previous (including 0) with "*"
grep "t*est" test.txt
You can match the end of the line with "$"
grep "t$" test.txt
You can make something optional by using "?"
grep "t\?test" test.txt
After reading everything I would like to challenge your knowledge with a simple exercise. Write a pattern that matches HTML tags. Have fun.
Congratulations, you have started your journey of mastering Regex.There are a few sites which can help you learn more about Regex and create more complicated patterns.
My favourite site is: https://regexr.com/