(。U ω U。) (̲̅r̲̅)(̲̅e̲̅)(̲̅g̲̅)(̲̅e̲̅)(̲̅x̲̅)(︶︿︶)

by | Dec 26, 2023 | Glosary

Tips / (。U ω U。) (̲̅r̲̅)(̲̅e̲̅)(̲̅g̲̅)(̲̅e̲̅)(̲̅x̲̅)(︶︿︶)

From time to time I must use regular expressions (Regex) for different reasons, and every time I am going to use them, I must verify how they are done depending on the case. Despite how often I have to use these types of expressions, I cannot learn by heart how they are used and the truth is that I don’t want to do it either, because I am not in the habit of memorizing information that I can easily review on the Internet and adapt according to what I need in the world. moment. However, an easy guide wouldn’t hurt as it would save me time. That’s why I decided to make this quick guide or list of the most used regular expressions, and share it here so that anyone can use it.

Note: in all the examples I have used regex101.com to verify that it works according to the case:

Regex to validate numbers:

If you want to validate a single digit number you can use:

/\d/

but if you want to validate a 3-digit number (or N), you would use:

/\d{3}/ # or /\d{N}/

with N being the number of digits to be identified)

while if you want to validate a number, regardless of the number of digits, you should use:

/^\d+$/

Example:

123123 => 123123
123Lorem12 => Does not match

But if you use * instead of +$, it would take whatever number of digits are at the beginning of the character string you want to evaluate, and exclude any text that comes after: /^\d/

Example:

123123 => 123123
123oisado12 => 123

Another option would be to validate a real number, in which case the expression would be: /^\d.\d*/
Example:

1231.3242 => 1231.3242
7989hlk.098 => Does not match
12312.2344asda => 12312.2344

Take into account that in all the previous examples that begin with the character ^, it means that the pattern that you want to evaluate in the character string is at the beginning of it.

Regex to validate text:

For text validation the expression is used:

/\w/

This includes alphanumeric characters, but not special characters, such as *, !, #, etc.

Likewise, it is possible to use the expressions

/\d{N}/ # with N being an integer, to identify N characters, or
/^\w+$/

to identify a string of characters, regardless of its length. However, it does not include whitespace, special characters, or complete sentences with multiple words.

Example:

1365464asd123 => 1365464asd123
65464asd123123 => 65464asd123123
1231.3242 => Does not match
7989hlk.098 => Does not match
12312.2344asda => Does not match
!hello => Does not match

However, if the * character is used, the pattern will read what it gets before finding the special character:

Example:

21354654asdasd213123 => 21354654asdasd213123
1365464asd123 => 1365464asd123
65464asd123123 => 65464asd123123
1231.3242 => 1231
7989hlk.098 => 7989hlk
12312.2344asda => 12312
!hello => Does not match

Regex to validate time data:

In this case there are several cases that could be useful, for example, the case of the time in 12-hour format:

/((1[0-2]|0?[1-2]):([0-5][0-9]) ?([YaPp][Mm]))/

While if the format was 24 hours, the regex to use would be as follows:

/(([0-1][0-9]|2?[0-3]):([0-5][0-9]))/

In the case of dates, more special conditions must be taken into account, considering that not all months have the same number of days. So, the expression we would use in this case would be:

/^(?:3[0-1]|[1-2][0-9]|0[1-9])([-\/.])(?:0[1-9]|1 [1-2])([-\/.])(\d{4})$/
^(?:(?:3[0-1]|[1-2][0-9]|0[1-9])[-\/.](?:0[1,3,5, 7.8]|1[0,2])<a href="\d{4}">-\/.</a>|(?:30|[1-2][0-9]|0[1-9]) <a href="?:0[2,4,6,9]|11">-\/.</a><a href="\d{4}">-\/.</a>|(?:[1-2][0-9 ]|0[1-9])[-\/.]02<a href="\d{4}">-\/.</a>)$

However, this version does not check if the date corresponds to a leap year.

Regex to validate emails

For email addresses it is possible to use the following expression:

/[\w._%+-]+@[\w.-]+.[a-zA-Z]{2,4}/

Regex to validate URL

In the case of URLs we could use the following expression:

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

Other tips

On some occasions it could be useful to assign a name to a particular group, and this can be done using ? and it must be framed by the group delimiter being used, be it [] or (), and first the regex used to define the pattern, such as:

[(?.?)] (?.?)