Register to get access to free programming courses with interactive exercises

Character representation. Metacharacter. Regular Expressions (Regexp)

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

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

For clarity, in the examples in this course, we'll put the string pattern with regular expressions at the top and the strings we're searching for at the bottom. We recommend using online regular expression editors such as regex101, regexr or others to run all the examples yourself. Choose a JavaScript or PCRE engine for examples that don't support JS.

Let's set a pattern with the characters we want to find in the text line:


java \ python ruby1.9 javascript c#

We can see that two variants from the string have been matched with the java text. This kind of match is called an exact match; we didn't use special characters and found combinations of characters in the text that were a 100% match for the combination given.

If we add another character, there'll be no match, because there's no such substring in our string.


java \ python ruby1.9 javascript c#

In order to understand the difference between the characters we're looking for in the text and the special characters, let's try to find any character in the text. To do this we use a metacharacter - a period "." :


java \ python ruby1.9 javascript c#

The entire string was highlighted.

Now let's combine the characters in the string pattern. Let's try to find combinations of any character with y. Looks like there are two matches:


java \ python ruby1.9 javascript c#

When combining special and ordinary characters, we need to take into account the fact that special characters have particular behaviour. Notice the example below, where we've written the characters 1.9. It's important to understand that they don't 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're 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 normal punctuation mark rather than a special character in regular expression language, we need 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 won't 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'll see the following 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

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Development of front-end components for web applications
start anytime 10 months

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.