Python编程:怎么使用re模块compile()方法

时间:2024-10-11 21:34:36

1、首先在PyCharm软件中,打开一个Python项目。

Python编程:怎么使用re模块compile()方法

2、在Python项目中,新建并打开一个空白的python文件(比如:test.py)。

Python编程:怎么使用re模块compile()方法

3、在python文件编辑区中,输入:“import re”,导入 re 模块(即:正则表达式操作模块)。

Python编程:怎么使用re模块compile()方法

4、输入:“text = '1234 hello 3456'”,点击Enter键。

Python编程:怎么使用re模块compile()方法

5、接着输入:“pattern = r'\d\d\d\d'”,点击Enter键。

Python编程:怎么使用re模块compile()方法

6、再输入:“prog = re.compile(pattern)”,返回一个正则对象。

Python编程:怎么使用re模块compile()方法

7、继续输入:“result = prog.match(text)”,查找匹配项。

Python编程:怎么使用re模块compile()方法

8、然后输入:“print(result)”,打印出相关数据结果。

Python编程:怎么使用re模块compile()方法

9、在编辑区域点击鼠标右键,在弹出菜单中选择“运行”选项。

Python编程:怎么使用re模块compile()方法

10、在运行结果窗口中查看运行结果,可以看到已经成功地使用re模块compile()方法。

Python编程:怎么使用re模块compile()方法
© 手抄报圈