python_re模块 —— 正则表达式操作

1. 正则表达式语法

正则表达式语法

2. 常用的正则表达式处理函数

re.match尝试从字符串的开始匹配一个模式

import re
text = "Elaine is a beautiful girl, she is cool,clever, and so on..."

#match从字符串的开始匹配一个模式
m = re.match(r"(\w+)\s",text)

if m:
    print m.group()
else:
    print 'not match'

re.search 从字符串的任一位置去匹配一个模式

re.match("c","abcdef")    # No Match
re.search("c", "abcdef")  # Match

re.sub 替换字符串中的匹配项

re.sub(r'\s+','-',string)

#将string串中的空格替换成"-"

re.split 分割字符串

#将字符串按空格分割成一个单词列表
re.split(r'\s+',text)

re.findall 获取字符串中所有匹配的串

#匹配所有包含o的单词
re.findall(r’\w*o\w*',text)

>>>['cool', 'so', 'on']

re.complie 把正则表达式编译成一个正则表达式对象,可以复用该正则表达式

import re

text = "Elaine is a beautiful girl, she is cool,clever, and so on..."
pattern = re.compile(r'\w*o\w*')
newstring = lambda m: '[' + m.group(0) + ']'
print pattern.findall(text)   
print pattern.sub(newstring, text) 
#将单词中包含o的用“[]"括起来

4. 正则表达式实例

实例1.  
teststr1 = "800-123-4234"
pattern1 = re.compile(r'^\d{3}-\d{3}-\d{4}$')
print pattern1.findall(teststr1)
实例2. 
teststr2 = "email: elaine@163.com, email: xx_xx@sina.org"
pattern2 = re.compile(r"\w+:\s+\w+@\w+\.(?:org|com|net)")          #()中的?:表示括号内的内容不做为分组
print pattern2.findall(teststr2)

实例3.

teststring=["HELLO world","hello world!","hello world"]

expressions = ["hello","HELLO"]

for string in teststring:

    for expression in expressions:

        if re.search(expression,string):

            print expression , "found in string" ,string

        else:

            print expression , "not found in string" ,string


         

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值