1、打开JUPYTER NOTEBOOK,新建一个PY文档。
2、import re首先我们必须先引入regex模块,简写re即可。不然后面全部都会出错。
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到。
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())引入文本,找到文本里面的格式,并且打印出来。
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()找到的结果可以以组的形式展现出来,我们可以选择打印相应的组,用数字来找到即可。
6、find.groups()a, b, c = find.groups()print(a)print(b)print(c)如果用groups,那么打出来的结果更加会清晰一点。
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()如果我们要找到的格式里有括号,那么要\(,\)来表示。不然会出错。