4

Creating a birthdate checking regex

 2 years ago
source link: https://gist.github.com/K-Owell/b75952b0f7d79f51af833ca96fcd67b2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Creating a birthdate checking regex · GitHub

Instantly share code, notes, and snippets.

Creating a birthdate checking regex

# Regex Tutorial - Birth date checker

Regular expressions (regex) can be incredibly daunting at first glance. It's easy to stare at one and just watch the characters all blend together into a meaningless gibberish. To help make sense of it all, I've created a regex that verifies birth dates. Anything from 01-01-1900 to 12-31-2099 will be seen as valid. Each component will be broken down and explained in detail so that by the end it won't just look like your cat walked across your keyboard.

## Summary

This regex is an easy way of verifying the date of birth a user imputs to make sure it fits into most scenerios. There are a couple of checks it is unable to do, such as checking for the correct number of days in every month to avoid something like the 30th of Febuary from being entered, however, it is an incredibly simple and functional tool to assist the early developer. `/^(?:0[1-9]|1[0-2])([\/.-])(?:0[1-9]|[12]\d|3[01])\1(?:19|20)\d\d$/`

## Table of Contents

- [Anchors](#anchors) - [OR Operator](#or-operator) - [Character Classes](#character-classes) - [Escape Characters](#escape-characters) - [Grouping and Capturing](#grouping-and-capturing) - [Bracket Expressions](#bracket-expressions) - [Back-references](#back-references)

## Regex Components

### Anchors Here we only use two anchors, which form together as one. Adding `^` at the beginning normally tells the system to look for any string that begins with whatever character follows. Adding `$` to the end would match any string that ends with whatever character comes before. When we use these together, we are now looking for an exact match. Everything between the characters `^` and `$` is now checked as one string.

### OR Operator There are two forms of OR operators in use here. You'll notice the use of `0[1-9]` in both the month and day sections. This is telling the system that the number will begining with zero, but the characters following that zero can be one, or two, and so on. This statement is immediately followed by another OR operator, the solid line `|`. When you see this character, it means that the previous operator does not always have to be true, and that the following operator can be true instead. This is how we determine a month or day that goes beyond '09'. The combination of these two OR operators mean the following: The digits in the month section can be between 01 and 09, OR, between 10 and 12.

### Character Classes We only use one character class in this operation, and that is the common `\d` class. Whenever you see `\d`, it means any character that is a digit, which is not to be confused with its opposite `\D`, which means any character that is NOT a digit. This operator is used twice within our regex and only at the end of the year section The preceeding OR operator tells the system to look for a year that starts with either 19 or 20, which prevents the user from using a birth year of 1899 or 2100 and so on. After this, you will see `\d\d` which means digit digit. Because the bith years can range so much, it saves time and space by leaving it open to any digits.

### Escape Characters There are times where you will want to use certain special characters in a more literal sense. In our example above, we want to use the characters `/`, `.`, and `-` to seperate the month, day, and year sections of the birth date. These can all be used in code as special characters, so to make them a little less special, we add `\` to the beginning. This tells the regex to look at them as literal, rather than special. This is where we get `([\/.-])`.

### Grouping and Capturing In this regex, we have two examples of grouping: Capturing and no-capturing. As you can imagine, the use of `()` almost always implies that anything written within is one group. That is no different here. Where things change is when you want to capture that group for future use, or more specifically, when you don't. `()` on its own is a capturing group. This means that you can use a back-reference to call it at another time. This can be very useful, but in this situation we have four total groups and only one of them is needed more than once. To make things a little easier and cleaner, we change these groups to show `(?:)` to mean non-capturing. This prevents a back-reference from calling on it. Now, the group `([\/.-])` is the only captured group.

### Bracket Expressions These expressions are probably the easiest to understand at a glance. `[123]` will match a string that has either 1, 2, or 3. You can use these to check a range of digits such as `[0-9]` as we see in the regex above, or letters with `[a-z]` and `[A-Z]`. We make use of these expressions liberally as it is the cleanest way to check for the number ranges we want.

### Back-references Let's back-reference to the grouping and capturing section. Remember how we only captured a single group out of the four we created? It's always a waste of time and space to repeat yourself in your code. Whenever you need a group you've made previously, simply use `\1`. This literally means "repeat group 1", which will reference to `([\/.-])` as it is the first (and only) captured group. If we captured every single group, we would have to count the number of groups within the regex and apply the back-reference accordingly. It certainly isn't required, but any future dev who looks at your code will thank you for using it.

## Author

Written by Kyle McDowell https://github.com/K-Owell


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK