This page has been robot translated, sorry for typos if any. Original content here.

8 useful regexpov with visual analysis

On this topic:


On the power and flexibility of regular expressions written a lot, and their use has long been a standard for various kinds of operations on the text consisting of letters, numbers and service sivmols. Perhaps, most often, regexp works with validation (verification) of input data - here they have virtually no alternative, except for cumbersome cyclical analysis functions with a bunch of unobvious and incomprehensible checks.

1. Part of the CNC (human-to-human URL)

Полезные регэкспы с наглядным разбором

Pattern:

  / ^ [a-z0-9 -] + $ / 

In fact, the word with hyphens.

2. User's

Полезные регэкспы с наглядным разбором

Pattern:

  / ^ [a-z0-9 _-] {3,16} $ / 

Letters, numbers, dashes and underscores, from 3 to 16 characters.

3. Password

Полезные регэкспы с наглядным разбором

Pattern:

  / ^ [a-z0-9 _-] {6.18} $ / 

Same as user, only from 6 to 18.

More briefly, / ^ [\ w _] {6.18} $ / .

Similarly for user.

4. Hex Color

Полезные регэкспы с наглядным разбором

Pattern:

  / ^ #? ([a-f0-9] {6} | [a-f0-9] {3}) $ / 

The symbol # (optional), then the word consisting of letters from a to f or digits, length 3 or 6.

5. XML tag

Полезные регэкспы с наглядным разбором

Pattern:

 /^<([az]+)([^>]+)*(?:>(.*)<\/\1>|\s+\/>)$/ 

The opening bracket <must be a word from the letters - the name of the element, then there may be attributes - any characters other than the closing bracket>. Next - any text (content) and a closing tag, i.e. <name />, or at least one space, a slash and a closing parenthesis (self-closing tag).

6. Email

Полезные регэкспы с наглядным разбором

Pattern:

  /^([a-z0-9_\.-]+)@([a-z0-9_\.-]+)\.([az\.]{2,6})$/ 

General view - login@domain.domain. Login, as well as subdomain - words from letters, numbers, underscores, hyphens and points. A domain (meaning the 1st level) is from 2 to 6 letters and dots.

It is possible more shortly - /^([\w\._]+)@\1\.([az]{2,6}\.?)$/ .

This is also a little more correct - a point in the first-level domain can meet only once and only at the end.

7. URL

Полезные регэкспы с наглядным разбором

Pattern:

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

The first thing is an optional protocol (http: // or https: //), then a sequence of letters, numbers, hyphens, underscores and dots (domains of level> 1), then a zero-level domain (from 2 to 6 letters and dots) Finally, the file structure is a set of words from letters, numbers, hyphens, underscores and dots with a slash at the end. All this can end again with a slash.

Better so - /^(https?:\/\/)?([\w\.]+)\.([az]{2,6}\.?)(\/[\w\.]*) * \ /? $ /

8. IP address

Полезные регэкспы с наглядным разбором

Pattern:

  / ^ (? :( ?: 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) \.) {3} (?: 25 [0- 5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) $ / 

4 groups of digits (from 1 to 3 digits in each) are separated by dots. If the group consists of 3 characters, the first one is 1 or 2; if 1, then the rest from 0 to 9, and if 2 - then the second from 0 to 5; if the second character is from 0 to 4, then the third character is from 0 to 9, and if the second 5 is the third from 0 to 5. If the group consists of 2 symbols, the first one is from 1 to 9, the second from 0 to 9 In the case of a single-character group, this symbol can be a number from 1 to 9.

So it is more correct - / ^ (? :( ?: 25 [0-5] | 2 [0-4] \ d | [01]? \ D \ d?) \.) {3} (?: 25 [0-5] | 2 [0-4] \ d | [01]? \ d \ d?) $ / .