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
/
java
\ python ruby1.9 java
script 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.
/javab
/
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:
/.y
/
java \ py
thon ruby
1.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:
/1.9
/
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.
/1\.9
/
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#
The Hexlet support team or other students will answer you.
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.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
From a novice to a developer. Get a job or your money back!
Sign up or sign in
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.