Register to get access to free programming courses with interactive exercises

Character classes Regular Expressions (Regexp)

In this lesson, we'll look at character classes.

A character class is a special designation that specifies a search for any character from a particular set.

Let's look at a simple example of how character classes work. Suppose we only need to find letters from the alphabet. To do this, you can use character classes, which are described in square brackets, in our case that's the English alphabet: [a-z]. We can see that all alphabetical characters in the string are highlighted:


/[a-z]/

java 11_34-1938 tab

new line


You can search for numbers from zero to nine in the same way:


/[0-9]/

java 11_34-1938 tab

new line


And in this example, we'll specify just 2 characters, each of which will be found:


/[aj]/

java 11_34-1938 tab

new line


With character classes, you can use a mechanism called negation. If we put the character ^ before the first character in square brackets, the search will be inverted and all the characters will be found except those listed after ^:


/[^aj]/

java 11_34-1938 tab

new line


If you need to find a hyphen as well as letters from the alphabet, then you just need to enter it at the beginning or end of a group of characters; that way it won't be perceived as a special character:


/[aj-]/

java 11_34-1938 tab

new line


Regular expressions often use special predefined character classes. They're written using the \ and have their own designations in the regular expression language. In the last lesson we used \ as an escape character. Here it's also used as part of the notation. Let's find all the digits in the text using \d:


/\d/

java 11_34-1938 tab

new line


If we specify a large D, the search will retrieve all other characters, including whitespace and tabs.


/\D/

java11_34-1938tab

new line


There's also the \s, class \S, which is used to search for whitespace characters, and “\S”, in turn, represents all non-whitespace characters. As we can see, the principle of character classes is simple: a lowercase letter denotes a class, and an upper case letter denotes everything that doesn't belong to it.

There's another popular class \w, which includes all letters of the alphabet, all numbers and underscores. The code below doesn't show it, but whitespace characters don't correspond to this class, nor does -.


/\w/

java 11_34-1938 tab

new line


\w is equivalent to this notation: [0-9a-zA-Z_]. Note that searches in character ranges are case-sensitive, so in this entry a-z followed by A-Z.

Accordingly, \W finds the opposite of \w. Here, hyphens and whitespace characters will be found:


/\W/

java 11_34-1938 tab

new line



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
130
courses
1000
exercises
2000+
hours of theory
3200
tests

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: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

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

Frontend Developer icon
Profession
beginner
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.