import re
# 使用 re.compile 编译正则表达式模式
pattern = re.compile(r'\d+') # \d+ 匹配一个或多个数字
# 示例字符串
text = "There are 123 apples and 456 oranges."
# 使用编译后的模式进行匹配
matches = pattern.findall(text)
# 输出匹配结果
print(matches) # 输出: ['123', '456']
re.compile(r'\d+')
:编译一个正则表达式模式,\d+
表示匹配一个或多个数字。pattern.findall(text)
:使用编译后的正则表达式模式在字符串 text
中查找所有匹配项,并返回一个列表。print(matches)
:输出所有匹配到的数字。下一篇:python console
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站