加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式Reguler Expression (with python re)

发布时间:2020-12-14 06:43:05 所属栏目:百科 来源:网络整理
导读:在Python中需要通过正则表达式对字符串进行匹配的时候,可以使用一个模块,名字为re // ‘match’ scan a string start from the very left,if it not match,it will break #coding=utf-8 # 导入re模块 import re # 使用match方法进行匹配操作 result = re.m

在Python中需要通过正则表达式对字符串进行匹配的时候,可以使用一个模块,名字为re
//‘match’ scan a string start from the very left,if it not match,it will break

#coding=utf-8
# 导入re模块
import re
# 使用match方法进行匹配操作
 result = re.match(正则表达式,要匹配的字符串)
# 如果上一步匹配到数据的话,可以使用group方法来提取数据
result.group()

Match Character

s=r'w{4,20}@(163|126|qq).com'

search

This function will search in the whole string,and find only one result

findall
This function will search all the string satisfy the requirement.

search vs findall

sub
Substitute the string required
1.simple replace

2,Use function to replace the specified string

split

贪婪和非贪婪
Python里数量词默认是贪婪的(在少数语言里也可能是默认非贪婪),总是尝试匹配尽可能多的字符;
非贪婪则相反,总是尝试匹配尽可能少的字符。

在”*”,”?”,”+”,”{m,n}”后面加上?,使贪婪变成非贪婪。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读