如何使用PYTHON里的re.compile()
1、打开JUPYTER NOTEBOOK,新建一个PY文档。

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到。

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()找到的结果可以以组的形式展现出来,我们可以选择打印相应的组,用数字来找到即可。

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()如果我们要找到的格式里有括号,那么要\(,\)来表示。不然会出错。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:40
阅读量:50
阅读量:81
阅读量:39
阅读量:36