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
/
java
\ python ruby1.9 java
script 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:
/javab
/
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:
/.y
/
java \ py
thon ruby
1.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:
/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 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:
/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 see this match:
/\\
/
java \
python ruby1d9 javascript c#
Recommended materials
Are there any more questions? Ask them in the Discussion section.
The Hexlet support team or other students will answer you.
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.