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

推荐订阅源

Vercel News
Vercel News
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
C
Check Point Blog
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
U
Unit 42
WordPress大学
WordPress大学
B
Blog
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
D
DataBreaches.Net
G
Google Developers Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家

博客园 - 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的用法