[Diaoye learns programming] MicroPython manual built-in module re: the function of regular expressions

Insert image description here

MicroPython is a lightweight version of the interpreter designed to run the Python 3 programming language in embedded systems. Compared with regular Python, the MicroPython interpreter is small (only about 100KB) and is compiled into a binary Executable file to run, resulting in higher execution efficiency. It uses a lightweight garbage collection mechanism and removes most of the Python standard library to accommodate resource-constrained microcontrollers.

The main features of MicroPython include:
1. The syntax and functions are compatible with standard Python, making it easy to learn and use. Supports most of Python's core syntax.
2. Directly access and control the hardware, control GPIO, I2C, SPI, etc. like Arduino.
3. A powerful module system that provides functions such as file system, network, and graphical interface.
4. Support cross-compilation to generate efficient native code, which is 10-100 times faster than the interpreter.
5. The amount of code is small, and the memory usage is small, which is suitable for running on MCU and development boards with small memory.
6. Open source license, free to use. The Shell interactive environment provides convenience for development and testing.
7. The built-in I/O driver supports a large number of microcontroller platforms, such as ESP8266, ESP32, STM32, micro:bit, control board and PyBoard, etc. There is an active community.

The application scenarios of MicroPython include:
1. Quickly build prototypes and user interactions for embedded products.
2. Make some small programmable hardware projects.
3. As an educational tool, it helps beginners learn Python and IoT programming.
4. Build smart device firmware to achieve advanced control and cloud connectivity.
5. Various microcontroller applications such as Internet of Things, embedded intelligence, robots, etc.

Pay attention to the following when using MicroPython:
1. The memory and Flash space are limited.
2. The explanation and execution efficiency is not as good as C language.
3. Some library functions are different from the standard version.
4. Optimize the syntax for the platform and correct the differences with standard Python.
5. Use memory resources rationally and avoid frequently allocating large memory blocks.
6. Use native code to improve the performance of speed-critical parts.
7. Use abstraction appropriately to encapsulate underlying hardware operations.

Generally speaking, MicroPython brings Python into the field of microcontrollers, which is an important innovation that not only lowers the programming threshold but also provides good hardware control capabilities. It is very suitable for the development of various types of Internet of Things and intelligent hardware.

Insert image description here
MicroPython's built-in module re is a module used to implement simple regular expression operations. It can be used to perform pattern matching, replacement, splitting and other operations in strings. Its main features are:

1. It can use the compile() method to compile a regular expression string into a regular expression object to improve matching efficiency and reusability.
2. It can use the match() and search() methods to find the first matching position in a string, or use the sub() method to replace all matching substrings in a string.
3. It can use the split() method to split a string into multiple substrings based on a regular expression.
4. It can use group(), groups(), start(), end(), span() and other methods to obtain detailed information of the matching object, such as matching substrings, groups, positions, etc.
5. It supports some common regular expression syntax, such as character sets, quantifiers, grouping, selection, escaping, etc., but does not support some advanced syntax, such as named groups, non-capturing groups, assertions, etc.

The application scenarios of the re module include:

1. Used to perform some text processing or analysis tasks, such as verifying input formats, extracting information, cleaning data, etc.
2. Used to implement some complex string operations or logic, such as password checking, template replacement, syntax analysis, etc.
3. Used to learn or teach some knowledge or skills related to regular expressions, such as metacharacters, greedy and non-greedy, backtracking, etc.

Notes on the re module include:

1. The re module implements a subset of the CPython module1 and does not support the escape character itself1, so it is not fully compatible with the functions and performance of CPython.
2. The re module uses a syntax 12 based on POSIX extended regular expressions, which may be different from other languages ​​or tools. You need to pay attention to syntax differences and compatibility issues.
3. The callback function used by the re module is executed 1234 in the interrupt context. It needs to be as short and fast as possible to avoid performing complex or time-consuming operations to avoid affecting system performance and stability.

The following are several practical application examples of MicroPython's built-in module re:

Case 1: Use the re.match() method to verify the email address format

# 导入re模块
import re

# 定义一个邮箱地址格式的正则表达式
email_regex = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'

# 编译正则表达式为一个正则表达式对象
email_pattern = re.compile(email_regex)

# 定义一个函数,用于验证邮箱地址格式
def validate_email(email):
    # 调用match()方法,在邮箱地址中查找第一个匹配
    match = email_pattern.match(email)
    # 如果找到了匹配,则返回True
    if match:
        return True
    # 否则返回False
    else:
        return False

# 测试函数
# 使用一些有效和无效的邮箱地址进行测试,并打印结果
emails = ['[email protected]', '[email protected]', 'charlie@invalid', '[email protected].']
for email in emails:
    print(email, validate_email(email))

Case 2: Use the re.sub() method to replace sensitive words

# 导入re模块
import re

# 定义一个敏感词列表
bad_words = ['stupid', 'idiot', 'fool']

# 将敏感词列表连接成一个正则表达式字符串,并添加边界符
bad_words_regex = r'\b(' + '|'.join(bad_words) + r')\b'

# 编译正则表达式为一个正则表达式对象
bad_words_pattern = re.compile(bad_words_regex, re.IGNORECASE)

# 定义一个函数,用于替换敏感词
def censor(text):
    # 调用sub()方法,在文本中替换所有匹配的敏感词为'*'号,并返回新的文本
    return bad_words_pattern.sub('*', text)

# 测试函数
# 使用一些包含敏感词的文本进行测试,并打印结果
texts = ['You are so stupid!', 'He is an idiot.', 'Don\'t be a fool.']
for text in texts:
    print(censor(text))

Case 3: Use the re.split() method to split words

# 导入re模块
import re

# 定义一个非单词字符的正则表达式
non_word_regex = r'\W+'

# 编译正则表达式为一个正则表达式对象
non_word_pattern = re.compile(non_word_regex)

# 定义一个函数,用于分割单词
def split_words(text):
    # 调用split()方法,根据非单词字符将文本分割为多个单词,并返回一个列表
    return non_word_pattern.split(text)

# 测试函数
# 使用一些包含非单词字符的文本进行测试,并打印结果
texts = ['Hello, world!', 'This is a test.', 'MicroPython is awesome.']
for text in texts:
    print(split_words(text))

Case 4: Match string pattern:

import re

# 定义一个字符串
text = "Hello, my name is John. I live in New York."

# 使用正则表达式匹配名字
pattern = r"my name is (\w+)"
match = re.search(pattern, text)

if match:
    name = match.group(1)
    print("Name:", name)
else:
    print("Name not found.")

In this example, we use the re module’s search() function to match a specific pattern within a string. We define a string text, and then use the regular expression pattern my name is (\w+) to match the name in it. If the match is successful, we extract the matching name and print it. If the match fails, a name not found message is printed.

Case 5: Replace the content in the string:

import re

# 定义一个字符串
text = "Hello, my name is John. I live in New York."

# 使用正则表达式替换名字
pattern = r"John"
replacement = "Michael"
new_text = re.sub(pattern, replacement, text)

print("New Text:", new_text)

In this example, we use the sub() function of the re module to replace specific content in a string. We define a string text and then use the regular expression pattern John to match the name in it. We then replace the matched name with Michael and store the result in the new_text variable. Finally, we print out the replaced string.

Case 6: Split string:

import re

# 定义一个字符串
text = "apple, banana, orange, mango, kiwi"

# 使用正则表达式分割字符串
pattern = r",\s*"
fruits = re.split(pattern, text)

print("Fruits:", fruits)

In this example, we use the split() function of the re module to split a string based on a specific pattern. We define a string text that contains multiple fruit names separated by commas and spaces. Then, we use the regex pattern, \s* to match commas and possibly multiple spaces. With the split() function, we split the string into a list of fruit names and store the result in the fruits variable. Finally, we print out the list of fruits. These practical application examples demonstrate the functionality of using MicroPython's built-in module re. By using the re module, you can match string patterns, replace string contents, and split strings. These functions are very useful in scenarios such as text processing, data cleaning and extraction.

Case 7: Regular expression matching

import re

# 匹配邮箱地址
email = "[email protected]"
pattern = r"\w+@\w+\.\w+"
match = re.match(pattern, email)
if match:
    print("邮箱地址有效")
else:
    print("邮箱地址无效")

In this example, we imported the re module and used it for regular expression matching. We define an email address email, and then use the regular expression pattern r"\w+@\w+.\w+" to match the email address. We use the match() function to perform a match and check if there is a match. If there is a matching result, it means that the email address is valid, and we print out "Email address is valid"; otherwise, it means that the email address is invalid, and we print out "Email address is invalid." This example shows how to use the re module for regular expression matching.

Case Eight: String Segmentation

import re

# 分割字符串
text = "Hello,World! How are you?"
pattern = r"[,\s!]+"
result = re.split(pattern, text)
print("分割结果:", result)

In this example, we imported the re module and used it to split strings. We define a string text, and then use the regular expression pattern r"[,\s!]+" to split the string. This mode indicates that commas, spaces, and exclamation points can be used as delimiters. We use the split() function to do the splitting and store the result in the result variable. Finally, we print the segmentation result. This example shows how to use the re module to split strings.

Case 9: Regular expression replacement

import re

# 替换字符串中的数字
text = "I have 3 apples and 5 oranges."
pattern = r"\d+"
replacement = "X"
result = re.sub(pattern, replacement, text)
print("替换结果:", result)

In this example, we imported the re module and used it to perform regular expression replacement operations. We define a string text, which contains some numbers. We then use the regular expression pattern r"\d+" to search for numbers in the string. We use the sub() function to perform the substitution operation and replace the found number with the string "X". Finally, we print the replacement result. This example shows how to use the re module to perform regular expression replacement operations.

These examples show practical use of the re module in MicroPython. The re module provides support for regular expressions, including matching, splitting, and replacing functions. By leveraging the re module, you can perform complex pattern matching and processing of text in MicroPython.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/132775931