At least once in your life as a developer you will need a regular expression to solve some kind of filter / search / query on strings. Whenever this happened, you probably turned to the internet. This is why I decided to compile a mini list with some of the regular expressions that I have most used during my work. Make good use of it.
Regular expression | Description | Example |
---|---|---|
.*[0-9].* | Contains at least one number. | abc1def |
^[a-zA-Z\xE0\xE8\xE9\xF9\xF2\xEC\x27 ]{1,100}$ | Generic name with special chars. | Harry Pòtter |
^[a-zA-Z]{6}[0-9]{2}[a-zA-Z]{1}[0-9]{2}[a-zA-Z]{1}[0-9a-zA-Z]{3}[a-zA-Z]{1}$ | Taxcode/fiscal code. | BNCMRA80A01H501A |
^[a-zA-Z0-9,\s\xE0\xE1\xC0\xC1\xE8\xE9\xC8\xC9\xEC\xED\xCC \xCD\xF2\xF3\xD3\xD2\xF9\xFA\xD9\xDA\x60\x27-\x2A\x2D-\x2F ]+$ | Address. | Via Roma 1 |
^(0[1-9]|1[0-9]|2[0-9]|3[01])\/(0[1-9]|1[0-2])\/(19|20)[0-9]{2}$ | Italian date with slashes. | 30/12/1990 |
^(0[1-9]|1[0-9]|2[0-9]|3[01])-(0[1-9]|1[0-2])-(19|20)[0-9]{2}$ | Italian date with dashes. | 30-12-1990 |
^(0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-9]|3[01])-(19|20)[0-9]{2}$ | English date with dashes. | 12-30-1990 |
^[a-zA-Z\s\xE0\xE1\xC0\xC1\xE8\xE9\xC8\xC9\xEC\xED \xCC\xCD\xF2\xF3\xD3\xD2\xF9\xFA\xD9\xDA\x60\x27-\x2A\x2D-\x2F ]+$ | City. | Rome |
^[+]?[\s0-9]{5,20}$ | Phone with country prefix. | +393332222222 |
^[-+]?[0-9]*$ | Numbers (positive and negative. | -42 |
^[a-zA-Z0-9._+-\x21\x23\x24\x25\x26\x27\x2A\x2F\x3D \x3F\x5E\x60\x7B\x7C\x7D\x7E]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$ | Complex e-mail. | &abc!2/=@prova.com |
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$ | Simple e-mail. | sandman@sandbay.it |
(https?:\/\/)?(www.)?[-a-zA-Z0-9@:%.+~#=]{1,256}.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%+.~#?&/=]*)? | Url. | https://sandbay.it/ |
^[a-zA-Z0-9]{8,}$ | Document number. | abC123abD |
/^IT((?!002)[0-9]{3}E[0-9]{8,9}|002E[0-9]{7,8}A)$/ig | POD | IT012E34511111 |
/^((?!IT)[A-Z]{2}[A-Z0-9]{5,100}|IT[A-Z0-9]{25})$/gi | IBAN | PR12345asdfgasdasd |