惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
D
Docker
云风的 BLOG
云风的 BLOG
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
GbyAI
GbyAI
T
Tailwind CSS Blog
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
C
Check Point Blog
I
InfoQ
Microsoft Azure Blog
Microsoft Azure Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
小众软件
小众软件
N
Netflix TechBlog - Medium
B
Blog RSS Feed
腾讯CDC
aimingoo的专栏
aimingoo的专栏

博客园 - xiaobin80

The MBCS libraries were deprecated in Visual Studio 2013 and Visual Studio 2015 Hello MFC - The last little bit of work Hello MFC - Add Resource and About Dialog Hello MFC - The Message Map my first mfc application Development Environment Preparation Clang IDE的选择在Windows Visual Studio 2017 and Fx EOL 跟我学python(5)- 匹配数字 跟我学python(3)--- 带shell功能的hello world 跟我学python(2)- 第一个程序 跟我学python --- 搭建开发平台 Debian server 安装 debian 与 ubuntu 之 - sudo Try Value 计算文件CRC32数值 docker ce on Debian 使用hugo在gitee上写blog spring cloud gateway - RequestRateLimiter postman使用 改进的冒泡算法 Create User - mysql COM调用 – VB、PB J-Link clone问题 修复山寨版的J-Link
跟我学python(4)- 正则
xiaobin80 · 2026-02-03 · via 博客园 - xiaobin80

1. 引入模块

import re

2. 使用foreach循环

for iterating_var in sequence:
   statements(s)

1) 名称解释

-(1)list(列表)

可以理解为java的数组和列表的综合体。

3. 使用模块

1)sub方法

替换符合正则要求的字符串。

sub(pattern, repl, string, count=0, flags=0)

4. code

hellopython.py

'''
Created on Dec 12, 2018

@author: xiaobin
'''
import os
import re
#! /usr/bin/perl
#    @lines=`perldoc -u -f sin`;
#   foreach(@lines){
#       s/\w<([^>]+)/\U$1/g;
#       print;    
#   }
cmdPydoc = 'pydoc math.sin'
lines = os.popen(cmdPydoc, 'r', -1).readlines()

for echostr in lines :
    str1 = re.sub('([.]+){3}', "num", echostr, re.S)
    print(str1)
else:
    print("---- over ----")

​#### 1)关于os.system和os.popen

相同点:都是调用shell。

不同点:

  • os.system只关心结果;
  • os.popen更关心过程。

5. 效果图 - WSL1.0

install pydoc

sudo apt install python-dev-is-python3

command-prompt_out3

Reference:

  1. Python for Loop Statements
  2. Python - Lists
  3. pydoc的用法