Teach you to quickly learn the use of regular expressions in python programming

(a) Regular Expression:

A flexible framework for representing strings;

It is a way to define a set of strings in a simple and clear manner;

A powerful tool for expressing specific patterns or characteristics of text;

Used to determine the type or nature of a given string;

Common applications of regular expressions in text processing include:

  • Identifying the characteristics of specific text types, such as viruses;
  • Searching or replacing multiple strings at once;
  • Matching entire or partial parts of a string;

(b) Syntax of Regular Expressions

(c) Commonly Used Operators in Regular Expressions

(d) Examples of Regular Expressions

(e) Main Functions of the re Library

1. Regular Expression Representation Type

(1) Raw String Type (Native String Type)

The re library uses raw strings to represent regular expressions, denoted as r'text'.

For example: Chinese postal code: r'[1-9]\d{5}'

Domestic phone number: r'\d{3}-\d{8}|\d{4}-\d{7}'

A raw string is a string that does not contain escape characters.

(2) Normal String Type (More Complex)

For example: '[1-9]\\d{5}'

'\\d{3}-\\d{8}|\\d{4}-\\d{7}'

Therefore, when a regular expression contains escape characters, it's better to use a raw string.

1. re.search(pattern, string, flags=0)

Searches for the first occurrence of a match in the string and returns a match object.

Pattern: A regular expression string or a raw string.

String: The string to be matched.

Flags: Optional flags to control the matching behavior.

Code Example:

2. re.match(pattern, string, flags=0)

Attempts to match the pattern from the beginning of the string and returns a match object.

3. re.findall(pattern, string, flags=0)

Returns all non-overlapping matches of the pattern in the string as a list.

4. re.split(pattern, string, maxsplit=0, flags=0)

Splits the string by the occurrences of the pattern and returns a list.

Maxsplit: The maximum number of splits; the rest will be included in the last element.

5. re.finditer(pattern, string, flags=0)

Returns an iterator that yields match objects for all non-overlapping matches of the pattern in the string.

6. re.sub(pattern, repl, string, count=0, flags=0)

Replaces all occurrences of the pattern in the string with the replacement string.

Pattern: The regular expression to search for.

Repl: The string to replace the matched pattern.

String: The original string to perform the substitution on.

Count: The maximum number of substitutions to make.

(f) Additional Uses of the re Library

(g) Match Object

1. Properties of the Match Object

2. Methods of the Match Object

(h) Greedy vs. Minimal Matching in re Library

The re library uses greedy matching by default, meaning it matches the longest possible substring.

1. Minimal Matching Operator

FPV Drone

FPV Drone TrueX Frame Overview
Mark4 V2 Mark4 7inch 295mm / 8inch 367mm / 9inch 387mm / 10inch 427mm 3K Full Carbon Fiber TrueX Frame for FPV Camera Kit Done:
FPV drone frame 4FPV drone frame 2FPV drone frame 3

Carbon Fiber FPV Drone Frame,FPV Drone TrueX Frame,Carbon Fiber FPV Frame

Jiangsu Yunbo Intelligent Technology Co., Ltd , https://www.fmodel-ai.com