(a) Regular Expression:
A powerful tool used to define patterns for matching strings.
It is a way to describe a set of strings using a concise syntax.
Regular expressions are widely used in text processing to identify, extract, or manipulate specific patterns within text.
They help in determining whether a given string belongs to a certain category or follows a specific format.
Common applications of regular expressions in text include:
- Identifying the characteristics of a specific type of text (e.g., virus signatures).
- Searching or replacing multiple strings at once.
- Matching entire or partial strings based on defined rules.
(b) Syntax of Regular Expressions
Regular expressions use a special syntax that includes characters and operators to define patterns. Below are some commonly used symbols and their meanings:




(c) Commonly Used Operators in Regular Expressions


(d) Examples of Regular Expressions



(e) Main Functions of the re Library
1. Raw String Type (Native String)
The re library uses raw strings to represent regular expressions, denoted as r'pattern'. This helps avoid issues with escape characters.
Example: For a Chinese postal code: r'[1-9]\d{5}'
For a domestic phone number: r'\d{3}-\d{8}|\d{4}-\d{7}'
A raw string is a string that does not interpret backslashes as escape characters.
2. Normal String Type
This is less common because it requires double escaping for special characters.
Example: '[1-9]\\d{5}'
'\\d{3}-\\d{8}|\\d{4}-\\d{7}'
Therefore, it's recommended to use raw strings when dealing with regular expressions that contain escape characters.

1. re.search(pattern, string, flags=0)
Searches for the first occurrence of a pattern in a string and returns a match object.
2. re.match(pattern, string, flags=0)
Attempts to match the pattern from the beginning of the string and returns a match object if successful.

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. If not specified, all possible splits are made.


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.

(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
The re library defaults to greedy matching, meaning it will match the longest possible string.

1. Minimal Matching Operator


3K Carbon Fiber drone Frame,Quadcopter Freestyle RC FPV Drone Frame,FPV Drone Frame 7inch
Jiangsu Yunbo Intelligent Technology Co., Ltd , https://www.fmodel-ai.com