Home » Demos » Regular expressions by example

Regular expressions by example

Regular expressions are often used for data validation together with PHP, JavaScript and other programming languages. This examples below will show you real world regex patterns which can be used in PHP with function like preg_match() other PHP functions. If you need more information about particular regular expression try a search about "regex" inside Google. There are many more examples.

Use the form button and check for each pattern if some kind of value is valid or not. This is just a regex example, there is much more you can do with regular expressions.

All patterns are optimized to use with the PHP function preg_match (note the leading and the trailing slash).

Decimals

This decimal check allows a decimal number with max 5 digits before the "." and 1 or 2 decimals.

/^[1-9]{1}([0-9]{0,5})?\.[0-9]{1,2}$/

Dutch postal code

This postal code is a combination of four numbers and 2 letters (f.e. 1000 AB). With this pattern its allowed to use caps and small caps and a single space or not between numbers and letters.

/^[1-9]{1}[0-9]{3}\s?[a-zA-Z]{2}$/

E-mail address

This example checks real world e-mail addresses, but the pattern is limited to the lack of knowledge about the domain names and users. But the pattern takes care about unallowed characters and combinations.

/^[\w-]+(\.[\w-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i

Related PHP functions:

preg_match | preg_replace | preg_match_all | preg_quote

Do you like to do more with regular expressions? Check the preg_match_all() PHP tutorial on my web development blog.