DEV Community

Cover image for Getting started with Regex

Posted on

Getting started with Regex

What is Regex?

Regex or Regular Expression is a sequence of characters defining a search pattern. Mostly we use Regex to find and or replace text. Most of the programming languages (for example JavaScript or Python) has support for it, making it an important part of the programmers' life.

Regex must be really hard!!! Just look at it…

For the first sight, regular expressions might look a bit confusing but after understanding the basics it is fairly easy to understand and master.

Let’s look into regular expressions!

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: 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?

My first Regex

With regex you can just search for a text:

grep "test" test.txt

this will return:
Alt Text
You can see that it matched everything case-sensitively.

What are the most important things a beginner should learn?

Any character: "."

You can match any character with writing a "."

grep ".est" test.txt

Alt Text

Match any number of previous "*":

You can use it to match any number of the previous (including 0) with "*"

grep "t*est" test.txt

Alt Text

End of the line: "$":

You can match the end of the line with "$"

grep "t$" test.txt

Alt Text

Something is optional: "?":

You can make something optional by using "?"

grep "t\?test" test.txt

Alt Text

Other helpful things:

Escape character: "\"
Beginning of the line: "^"
Any lowercase letter: [a-z]
Any uppercase letter: [A-Z]
Any letter: [A-Za-z]

One final challenge:

After reading everything I would like to challenge your knowledge with a simple exercise. Write a pattern that matches HTML tags. Have fun.

Now I understand these, how can I learn more about Regex?

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:

If you want more write a comment and follow me, thanks!

Top comments (1)

imrebalint17 profile image

Very interesting article, maybe I will try it. Anyway have a good day!