首页 » Python正则表达式

Python正则基础

使用Python正则表达式需要先引入re模块:

import re

以下从几个小例子学习Python正则。目标字符串从man -k thread中摘录了几行:

s = """
pthread_spin_unlock  (3p)  - unlock a spin lock object (ADVANCED REALTIME THREADS)
pthread_testcancel   (3p)  - set cancelability state
set_thread_area      (2)  - Set a Thread Local Storage (TLS) area
pthread_mutexattr_destroy [pthread_mutexattr_init] (3p)  - destroy and initialize the mutex attributes object
signal.sys_tgkill.return [signal] (3stap)  - Sending kill signal to a thread group completed
signal.sys_tgkill [signal] (3stap)  - Sending kill signal to a thread group
"""

第一个目标,找到所有包括pthread_的行:

lines = re.findall("pthread_.*", s)
for line in lines:
    print(line)

输出:

pthread_spin_unlock  (3p)  - unlock a spin lock object (ADVANCED REALTIME THREADS)
pthread_testcancel   (3p)  - set cancelability state
pthread_mutexattr_destroy [pthread_mutexattr_init] (3p)  - destroy and initialize the mutex attributes object

把pthread函数抓取出来。

lines = re.findall("pthread_[^ \]]*", s)

输出:

pthread_spin_unlock
pthread_testcancel
pthread_mutexattr_destroy
pthread_mutexattr_init

把[]里和()的串提出来:

lines = re.findall("\[.*?\]", s)
lines = re.findall("\(.*?\)", s)

输出:

[pthread_mutexattr_init]
[signal]
[signal]
(3p)
(ADVANCED REALTIME THREADS)
(3p)
(2)
(TLS)
(3p)
(3stap)
(3stap)

把所有的pthread改为Pthread。

s = re.sub("pthread", "Pthread", s)

像这样的简单字符串替换应该用字符串类提供的方法,而不应该用正则。下面一个稍微复杂点的例子,进行这样一个转换:

pthread_spin_unlock  (3p)  - unlock a spin lock object (ADVANCED REALTIME THREADS)
---->
pthread_spin_unlock  (3p)
    unlock a spin lock object (ADVANCED REALTIME THREADS)

实现代码如下:

s = re.sub("([^-]*) - ([^\n]*)", "\g<1>\n\t\g<2>", s)

输出:

pthread_spin_unlock  (3p) 
        unlock a spin lock object (ADVANCED REALTIME THREADS)
pthread_testcancel   (3p) 
        set cancelability state
set_thread_area      (2) 
        Set a Thread Local Storage (TLS) area
pthread_mutexattr_destroy [pthread_mutexattr_init] (3p) 
        destroy and initialize the mutex attributes object
signal.sys_tgkill.return [signal] (3stap) 
        Sending kill signal to a thread group completed
signal.sys_tgkill [signal] (3stap) 
        Sending kill signal to a thread group

分享

0