第 3 章 怎么用正则提取数字3
使用正则表达式提取数字3的方法是使用\d表示数字,然后使用{3}表示连续出现3次。下面是一个示例代码:
import re
text = "1234567890"
pattern = r"\d{3}"
result = re.findall(pattern, text)
print(result)
输出结果为:['123', '456', '789'],表示从字符串中提取出连续出现的3个数字。
另外,还可以使用pattern = r"3"来提取出所有的数字3。代码如下:
import re
text = "1234567890"
pattern = r"3"
result = re.findall(pattern, text)
print(result)
输出结果为:['3'],表示从字符串中提取出所有的数字3。
原文地址: https://www.cveoy.top/t/topic/iFOU 著作权归作者所有。请勿转载和采集!