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

8 useful regexp with visual analysis

On this topic:


Much has been written about the power and flexibility of regular expressions, and their use has long been a standard for various kinds of operations on text consisting of letters, numbers, and service letters. Perhaps, most often, regexps work during validation (validation) of input data - there is practically no alternative to them, except for cumbersome cyclic parsing with functions with a bunch of unobvious and incomprehensible checks.

1. CNC part (human readable URL)

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

Pattern:

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

In fact, the word with hyphens.

2. UserName

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

Pattern:

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

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

3. Password

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

Pattern:

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

Same as username, only from 6 to 18.

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

Similarly for username.

4. Hex color

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

Pattern:

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

# Symbol (optional), then a word consisting of letters from a to f or numbers, 3 or 6 long.

5. XML tag

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

Pattern:

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

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

6. Email

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

Pattern:

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

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

It can be shorter - /^([\w\.__++ )@\1\ .( [az palm A2,6,6_ .

It is also slightly more correct - a point in the first-level domain can only occur once and only at the end.

7. URL

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

Pattern:

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

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

Better like that - / ^ ( https? : \/\/)?( ( \\\\\+)\. ( [az[22,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 numbers (from 1 to 3 numbers in each) are separated by dots. If the group consists of 3 characters, then 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 is from 0 to 9, and if the second is 5, then the third is from 0 to 5. If the group consists of 2 characters, then the first is from 1 to 9, the second is from 0 to 9 In the case of a single-character group, this symbol can be a digit 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?) $ / .