Register to get access to free programming courses with interactive exercises

Character representation. Metacharacter. Regular Expressions (Regexp)

Regular expressions are the formal language used to look for characters and manipulate substrings. It is a powerful tool that allows you to work efficiently with text.

Regular expressions are text strings that set a search pattern using the characters you want to find and special characters.

For clarity, in the examples in this course, we will put the string pattern with regular expressions at the top and the strings we are searching for at the bottom.

We recommend running all the examples yourself, using online regular expression editors such as regex101, regexr, or others. Choose a JavaScript or PCRE engine for examples that do not support JS.

Now, let us set a pattern with the characters we want to find in the text line:


java \ python ruby1.9 javascript c#

We can see two matches from the string with the java text. This kind of match is called an exact match.

We do not use special characters and found combinations of characters in the text that was a 100% match for the given combination.

If we add another character, we will have no match because there's no such substring in our string:


java \ python ruby1.9 javascript c#

Let us try to understand the difference between the special characters and the characters we are looking for in the text.

To do so, we will find any character in the text using a metacharacter (a period .):


java \ python ruby1.9 javascript c#

The entire string is highlighted.

Now we combine the characters in the string pattern. Let us try to find combinations of any characters with y.

Seems like there are two matches:


java \ python ruby1.9 javascript c#

Combining special and ordinary characters, we need to remember that special characters have particular behavior. Notice the example below, where we have written the characters 1.9. They do not correspond to the combination of characters 1.9 but to all substrings consisting of three characters, in which the first character is 1, and the last is 9.

Thus, there may be any number of any characters between the characters we are looking for:


java \ python ruby1.9 javascript c#

java \ python ruby189 javascript c#

java \ python ruby1k9 javascript c#

If we want to use a period as a punctuation mark rather than a special character in regular expression language, we have to use so-called escape characters.

To do this, put a backslash \ before the character that we want to escape so that it loses its special meaning. As you can see, everything works. If we put another character instead of a dot, our substring will not match the pattern one:


java \ python ruby1.9 javascript c#

java \ python ruby1d9 javascript c#

In the same way, we can escape the backslash itself if we want to use it as an ordinary character. As a result, we see this match:


java \ python ruby1d9 javascript c#

Recommended materials

  1. Regular Expressions
  2. Regular Expressions Cheat Sheet
  3. Online regular expression editor

Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
hours of theory

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs
Development of front-end components for web applications
10 months
from scratch
Start at any time

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.