Introduction to RegEx

As a developer you have probably already worked with Regular Expressions. If you’re like me and didn’t need them too often, you probably googled them up more than you wrote them yourself.

So today I’d like to go through the basics of Regex and show you how to build regular expressions and how to write them yourself.

If you haven’t had anything to do with this topic before, this post is still for you, of course. Regular Expressions are a way to check strings against a certain pattern, for example if it is alphanumeric or contains only uppercase letters. Regular expressions are supported by many different programming languages, so you don’t have to learn that again with different syntax.

I’d like to recommend the website, where you can test your Regular Expressions and see the individual components in detail.


Regex offers two symbols to mark the start (^) and the end ($) of a string. This makes the following patterns possible:

Upper and lower case letters must always be respected!

OR operator

Next, you can specify more precisely what characters you want to consider valid. Use the square brackets and place all of the allowed characters within them.

As you can see, the curly bracket always refers to only one sign. For example, if you want to check a string with exactly 5 characters to see if it is alphanumeric, the following spelling is a bit cumbersome:



With quantifiers you can limit the number of possible occurrences. There are the following possibilities:

Applied concretely, the whole thing can look like this:

If you want to apply the quantifier to more than one character, you can parenthesize the respective ones, the quantifier will then refer to their contents: AB(CD){3} - Match for each string containing AB and following CD 3 times thereafter. If you want to use a character that has a special meaning in Regex (for example *), you can mark it in your expression with an escape character (backslash, ).


In my example above, I already used “A-Za-z0-9” to check if a text is alphanumeric. Classes provide a way to check groups of characters more easily. The following classifications are available:

You can capitalize any of the classes to negate their meaning.


RegEx expressions are usually found within two slashes. After the closing slash, the scope of the pattern is defined, whereby the following options are available:

Advanced functions

To put it briefly: There are several additional options for defining regular expressions. Since I would like to limit this article to the basics, I will not go into the topic in detail here, but will show some practical examples. But if you are interested in the topic, I have some keywords for your own research:

However, you will also come across other topics during the search, but in my opinion the basics are sufficient for the time being. You can go into depth as soon as you really need it, otherwise it is very likely that you forget the theory before you get an opportunity to apply it.

Practical examples

Finally, here are a few examples that cover the most common requirements. Of course, you can find almost anything, like formatting credit card or phone numbers by a quick google search. But now you should be able to write something like that up all by your self.

Alphabetical (Upper- and Lowercase):





^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$

Dates (MM/DD/YYYY, 20th and 21st century):