Classes

A class instructs the regex parser to match one character from a number of possible options. Classes can contain lists of chars and ranges of chars.

Example

Let’s say we want to match HTML tags for either heading1(<h1>) or heading2(<h2>) tags.

If we are going to assume all our HTML is valid, then we’d need a regex that could match tags with either a 1 or 2 after the ‘h’.

Our full regex would be:

<h[12]>.+?</h[12]>

Which breaks down like this:

<h match the first part of the heading open tag
[12] match a 1 or a 2
> match the end part of the heading open tag
.+? match one or more chars lazily
</h match the first part of the heading close tag
[12] match a 1 or a 2
> match the end part of the heading close tag

More on Classes

Classes can:

be negative. Use a ‘^’ at the beginning of the class eg. [^12] would match any chars that were not ’1′ or ’2′

be nested with most regex flavors.

have additional rules depending on the regex flavor.

Textpression

Here are some of the ways you could view the completed regex in Textpression.

 

You can paste plain text regex straight into Textpression and get an immediate visualisation as above.

Or you can create regex yourself using the simple drag and drop editor.

If you’d like to see how easy Classes are in Textpression check out the video.