Regular Expressions (English name: regular expression,regex,re) are expressions that are used to succinctly Express a set of character strings. The most important applications are in string matching.
syntax for regular expressions -common operators:
Operator |
Description |
Instance |
. |
Represents any single character |
|
[ ] |
Character set, giving a range of values to a single character |
[ABC] means a, B, c,[a-z] represents a to Z single character |
[^ ] |
Non-character sets, exclusion ranges for individual characters |
[^ABC] Represents a single character that is not a or B or C |
* |
Previous character 0 or unlimited expansion |
abc* means AB, ABC, ABCC, ABCCC, etc. |
+ |
Previous character 1 or unlimited expansion |
abc+ means ABC, ABCC, ABCCC, etc. |
? |
The previous character 0 or 1 times extended |
Abc? Represents AB, ABC, |
| |
Left and right expression any one |
Abc|def means ABC, DEF |
{m} |
Extend one character m times |
Ab{2}c says ABBC |
{M,n} |
Extend one character M to n times (with N) |
Ab{1,2}c means ABC, ABBC |
^ |
Match string start |
^ABC represents ABC and at the beginning of a string |
$ |
Match string End |
abc$ represents ABC and at the end of a string |
( ) |
Grouping tags, which can only be used internally | Operator |
(ABC) means ABC, (ABC|DEF) means ABC, DEF |
\d |
number, equivalent to [0-9] |
|
\w |
Word character, equivalent to [a-za-z0-9] |
|
Regular expression that matches the IP address:
Regular expressions in the form of IP address strings (IP address divided into 4 segments, 0-255 per segment)
Exact wording 0-99:[1-9]?\d
100-199:1\d{2}
200-249:2[0-4]\d
250-255:25[0-5]
([1-9]?\d| 1\D{2}| 2[0-4]\d| 25[0-5]).) {3} ([1-9]?\d| 1\D{2}| 2[0-4]\d| 25[0-5])
Getting Started with Python re library (regular expression)