如何使用PYTHON里的re.compile()

时间:2026-02-12 10:34:06

1、打开JUPYTER NOTEBOOK,新建一个PY文档。

如何使用PYTHON里的re.compile()

2、import re

首先我们必须先引入regex模块,简写re即可。不然后面全部都会出错。

如何使用PYTHON里的re.compile()

3、aRegex = re.compile(r'\d\d\d-\d\d\d\d-\d\d\d\d')

首先我们要测试在字符串里面找到\d\d\d-\d\d\d\d-\d\d\d\d的文本。\d表示数字,0到。

如何使用PYTHON里的re.compile()

4、text = "I have a phone and the phone number is 159-9999-9999."

find = aRegex.search(text)

print("The phone number is: " + find.group())

引入文本,找到文本里面的格式,并且打印出来。

如何使用PYTHON里的re.compile()

5、aRegex = re.compile(r'(\d\d\d)-(\d\d\d\d)-(\d\d\d\d)')

text = "I have a phone and the phone number is 159-9999-9999."

find = aRegex.search(text)

find.group(1)

find.group(2)

find.group(3)

find.group(0)

find.group()

找到的结果可以以组的形式展现出来,我们可以选择打印相应的组,用数字来找到即可。

如何使用PYTHON里的re.compile()

6、find.groups()

a, b, c = find.groups()

print(a)

print(b)

print(c)

如果用groups,那么打出来的结果更加会清晰一点。

如何使用PYTHON里的re.compile()

7、text = "The number is (010)110-112."

bRegex = re.compile(r'(\(\d\d\d\))(\d\d\d-\d\d\d)')

find1 = bRegex.search(text)

find1.group(1)

find1.group(2)

find1.group()

如果我们要找到的格式里有括号,那么要\(,\)来表示。不然会出错。

如何使用PYTHON里的re.compile()

© 2026 途途旅游
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com