Python 正则表达式

1.正则表达式的定义:

 正则表达式是对字符串进行解析(获取一大串字符串信息中,你想要的部分)。

 正则表达式是一种文本模式,模式描述在搜索文本时要匹配的一个或多个字符串。

 正则表达式,又成正规表示式,正规表示法,正规表达式,规则表达式,常规表示法(英语:Regular Expression,在代码 中常简写为regex、regexp或RE),是计算机科学的一个概念,正则表达式使用带个字符串来描述,匹配一系列匹配某个句 法规则的字符串,在很多文本编辑器里,正则表达式通常被用来检索,替换那些匹配某个模式的文本。

 Regular Expression的"Regular"一般被译为“正则”、“正规”、“常规”。此处的“Regular”即是“规则”、“规律”的意思,Regular Expression即“描述某种规则的表达式”之意。

2.正则表达式的作用和特点

作用:

1.给定的字符串是否符合正则表达式的过滤逻辑(称作“匹配”);
2.可以通过正则表达式,从字符串中获取我们想要的特定部分。

特点:

1.灵活性、逻辑性和功能性非常强。
2.可以迅速地用极简单的方式达到字符串的复杂控制。
3.对于刚接触的人来说,比较晦涩难懂。

常用知识点:

()是为了提取匹配的字符串。表达式中有几个()就有几个相应的匹配字符串。
[]是定义匹配的字符范围。比如 [a-zA-Z0-9] 表示相应位置的字符要匹配英文字符和数字。
{}一般用来表示匹配的长度,比如 \s{3} 表示匹配三个空格,\s{1,3}表示匹配一到三个空格。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

实战练习理解

import re

msg='sdfawerwedfsfsd'

#1
pattern = re.compile('sdf')

pattern.match(msg)

result = pattern.match(msg)

print(result)

#2
#上面那个的封装版,只从开头进行匹配,若匹配不成功则返回None
print(re.match('sdf',msg)) 

#3
#search 进行正则字符串匹配方法,匹配的是整个字符串
result = re.search('we',msg)
print(result)

#4
#返回位置
print(result.span())

#a2b h6k

s='qwec5wewe3bew2we'

#search找到一个就停下来
result = re.search('[a-z][0-9][a-z]',s)
print(result.group())

#msg='abcd7zjkfd8hdf00'

#findall会一直找,直到匹配完
result = re.findall('[a-z][0-9][a-z]',s)
print(result)

# 正则符号

#a7a a88a a7878a
msg='a7aopa88ak jgkaa7878a'
# + 只要有1个或以上出现则符合 放在哪个后面就指哪个
result = re.findall('[a-z][0-9]+[a-z]',s)
print(result)

#qq号码验证 5~11 开头不能为0
# ^从哪里开头 #从哪里结尾
msg='92351007388'
result = re.match('^[1-9][0-9]{5,11}$',msg)
print(result)

#5
#用户名可以是字母或者数字,不能是数字开头,用户名长度必须在6位以上[0-9a-zA-Z]
username='s001adminda'
#第一位不能是数字 后面的只能是数字或字母 最后长度是5位以上 $以上述条件匹配到结尾
#result = re.match('[a-zA-Z][0-9a-zA-Z]{5,}$',username)
result = re.match('[a-zA-Z]\w{5,}$',username)
#result = re.search('[a-zA-Z][0-9a-zA-Z]{5,}$',username)
print(result)

msg='aa.py bb.py cc.py kk.py'
result = re.findall(r'\w*\.py\b',msg)
print(result)

'''
总结:
    .  任意字符 除了(\n)
    ^  开头
    $  从头匹配到结尾要完全一样
    [] 范围

正则预定义:
\s space 空白 (空格)
\b border 边界
\d digit 数字
\w word [0-9a-zA-Z_]

大写反面 \S 非空格 \D 非数字...
'\w [0-9]' --> \w 和 [0-9] 只能匹配一个字母

量词(跟在规则后面,表示字符串中一共有多少位沿用此规则) 举例:\w+  :
* 匹配前一个字符0次或无限次
+ 匹配前一个字符1次或无限次
? 匹配前一个字符0次或1次

手机号码正则
re.match('1[35789]\d{9}$',phone)

{m}表示范围,有多少位 固定m位
{m, } 长度大于等于m
{m,n} 长度介乎m,n之间
'''

#分组
#匹配数字0~100数字
n = '100'
#?前面可有可没有
#result = re.match('[1-9]?\d',n)
result = re.match(r'[1-9]?\d?$|100$',n)
print(result)

#(word|word|word)这个表示一个单词 区别 [abc]这个表示一个字母例如如果是[163] 则表示要么是1 要么是6 要么是3
#验证输入的邮箱 163 126 qq
email = '923510073@qq2.com'
#最后加上$整体判断
result = re.match(r'\w{5,20}@(163|126|qq)\.(com|cn)$',email)
print(result)

#不是以4.7结尾的手机号码(11位)
phone = '1590108867'
re.match(r'1\d{9}[0-35-689]$',phone)

#爬虫
phone = '0110-12345679'
#小括号为分组 (010)分为一组 - (123456789)分为一组
result = re.match(r'(\d{3}|\d{4})-(\d{8}$)',phone)
print(result)
#分别提取
print(result.group(1))
print(result.group(2))

#
msg1='<html>serwq</h2>'
msg='<h1>geeea</h1>'
# + 加号至少有一位 .任意一位
result = re.match(r'<[0-9a-zA-Z]+>(.+)</[0-9a-zA-Z]+>$',msg1)
print(result)
print(result.group(1))

# \number  \引用第number个分组
result = re.match(r'<([0-9a-zA-Z]+)>(.+)</\1>$',msg)
print(result)
#print(result.group(1))

msg3='<html><h1>ouou1</html></h1>'
result = re.match(r'<([0-9a-zA-Z]+)><([0-9a-zA-Z]+)>(.+)</\1></\2>$',msg3)
print(result)

#起名的方式 (?P<名字>正则) (? P=名字)
msg='<html><h1>abc</h1></html>'
result = re.match(r'<(?P<name1>\w+)><(?P<name2>\w+)>(.+)</(?P=name2)></(?P=name1)>',msg)
print(result)
print(result.group(1))
print(result.group(2))
print(result.group(3))

'''
    分组: () -->result.group(1) 获取组中匹配内容
    不需要引用分组的内容:
        result = re.match(r'<(?P<name1>\w+)><(?P<name2>\w+)>(.+)</(?P=name2)></(?P=name1)>',msg)
        print(result)
        print(result.group(1))
    引用分组匹配内容:
        1.number \number 引用第number组的数据
            msg='<html><h1>abc</h1></html>'
            result = re.match(r'<(?P<name1>\w+)><(?P<name2>\w+)>(.+)</(?P=name2)></(?P=name1)>',msg)
            print(result)
        
        2.?P<名字>

    re模块:
    match   从开头匹配一次
    search  只匹配一次
    findall 查找所有
    sub(正则表达式,'新内容','要被替换的内容')
    split result = re.split(r'[,:]','ouou1:123,ouou2:333')
    在字符串中搜索如果遇到 : 或者 , 就分割,将分割的内容保存在列表
'''

result = re.sub(r'[a-z]+','100','ouou1:123,ouou2:333')
print(result)

def func(temp):
    num = temp.group()#获取匹配到的字符串的值
    return str(int(num)+1)

result = re.sub(r'\d+',func,'ouou1:123,ouou2:333')
print(result)

result = re.split(r'[,:]','ouou1:123,ouou2:333')
print(result)

#默认是贪婪的,只要后面有符合条件的则无限地往后取符合条件的
#例:abc1 已经符合条件 但是还是会往后取abc123
msg = 'abc123abc'
#result = re.match(r'abc(\d+)',msg)
result = re.match(r'abc(\d+?)',msg) # 加上?后,变成非贪婪
print(result)


path = '<img src="https://static.oschina.net/uploads/img/201705/22114313_cGv0.png" style="width: 1007px;">'
result = re.search(r'src="(.*)"',path)
print(result.group(1))
import requests
response = requests.get(result.group(1))
with open('aa.png','wb')as wstream:
    wstream.write(response.content)

总结笔记:

'''
正则表达式:
re模块
import re

re.match(pattern,str)
re.search(pattern,str)
re.findall(pattern,str)
re.sub()(pattern,'新的内容',str) 替换
re.split(pattern,str)   -->[]

基础:
. 任意字符
[] 范围
| 或者
() 一组

量词:
* >= 0
+ >= 1
? 0,1
{m} =m
{m,} >=m
{m,n}  >=m <=n

预定义:
\s space
\S not space
\b border
\d digit
\D not digit
\w word[0-9a-zA-Z]
\W not word [^(0-9a-zA-Z)]

分组:
() --> group(1)

number
    (\w+)(\d*)  --> group(1) group(2)
    引用:
    (\w+)(\d*)    \1 \2  表示引用前面的内容

name
    (?P<name>\w+)   (?P=name)

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

非贪婪则相反,总是尝试匹配尽可能少的字符。

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

re.py 中原文解释

'''

r"""Support for regular expressions (RE).

This module provides regular expression matching operations similar to
those found in Perl.  It supports both 8-bit and Unicode strings; both
the pattern and the strings being processed can contain null bytes and
characters outside the US ASCII range.

Regular expressions can contain both special and ordinary characters.
Most ordinary characters, like "A", "a", or "0", are the simplest
regular expressions; they simply match themselves.  You can
concatenate ordinary characters, so last matches the string 'last'.

The special characters are:
    "."      Matches any character except a newline.
    "^"      Matches the start of the string.
    "$"      Matches the end of the string or just before the newline at
             the end of the string.
    "*"      Matches 0 or more (greedy) repetitions of the preceding RE.
             Greedy means that it will match as many repetitions as possible.
    "+"      Matches 1 or more (greedy) repetitions of the preceding RE.
    "?"      Matches 0 or 1 (greedy) of the preceding RE.
    *?,+?,?? Non-greedy versions of the previous three special characters.
    {m,n}    Matches from m to n repetitions of the preceding RE.
    {m,n}?   Non-greedy version of the above.
    "\\"     Either escapes special characters or signals a special sequence.
    []       Indicates a set of characters.
             A "^" as the first character indicates a complementing set.
    "|"      A|B, creates an RE that will match either A or B.
    (...)    Matches the RE inside the parentheses.
             The contents can be retrieved or matched later in the string.
    (?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).
    (?:...)  Non-grouping version of regular parentheses.
    (?P<name>...) The substring matched by the group is accessible by name.
    (?P=name)     Matches the text matched earlier by the group named name.
    (?#...)  A comment; ignored.
    (?=...)  Matches if ... matches next, but doesn't consume the string.
    (?!...)  Matches if ... doesn't match next.
    (?<=...) Matches if preceded by ... (must be fixed length).
    (?<!...) Matches if not preceded by ... (must be fixed length).
    (?(id/name)yes|no) Matches yes pattern if the group with id/name matched,
                       the (optional) no pattern otherwise.

The special sequences consist of "\\" and a character from the list
below.  If the ordinary character is not on the list, then the
resulting RE will match the second character.
    \number  Matches the contents of the group of the same number.
    \A       Matches only at the start of the string.
    \Z       Matches only at the end of the string.
    \b       Matches the empty string, but only at the start or end of a word.
    \B       Matches the empty string, but not at the start or end of a word.
    \d       Matches any decimal digit; equivalent to the set [0-9] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode digits.
    \D       Matches any non-digit character; equivalent to [^\d].
    \s       Matches any whitespace character; equivalent to [ \t\n\r\f\v] in
             bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the whole
             range of Unicode whitespace characters.
    \S       Matches any non-whitespace character; equivalent to [^\s].
    \w       Matches any alphanumeric character; equivalent to [a-zA-Z0-9_]
             in bytes patterns or string patterns with the ASCII flag.
             In string patterns without the ASCII flag, it will match the
             range of Unicode alphanumeric characters (letters plus digits
             plus underscore).
             With LOCALE, it will match the set [0-9_] plus characters defined
             as letters for the current locale.
    \W       Matches the complement of \w.
    \\       Matches a literal backslash.

This module exports the following functions:
    match     Match a regular expression pattern to the beginning of a string.
    fullmatch Match a regular expression pattern to all of a string.
    search    Search a string for the presence of a pattern.
    sub       Substitute occurrences of a pattern found in a string.
    subn      Same as sub, but also return the number of substitutions made.
    split     Split a string by the occurrences of a pattern.
    findall   Find all occurrences of a pattern in a string.
    finditer  Return an iterator yielding a Match object for each match.
    compile   Compile a pattern into a Pattern object.
    purge     Clear the regular expression cache.
    escape    Backslash all non-alphanumerics in a string.

Some of the functions in this module takes flags as optional parameters:
    A  ASCII       For string patterns, make \w, \W, \b, \B, \d, \D
                   match the corresponding ASCII character categories
                   (rather than the whole Unicode categories, which is the
                   default).
                   For bytes patterns, this flag is the only available
                   behaviour and needn't be specified.
    I  IGNORECASE  Perform case-insensitive matching.
    L  LOCALE      Make \w, \W, \b, \B, dependent on the current locale.
    M  MULTILINE   "^" matches the beginning of lines (after a newline)
                   as well as the string.
                   "$" matches the end of lines (before a newline) as well
                   as the end of the string.
    S  DOTALL      "." matches any character at all, including the newline.
    X  VERBOSE     Ignore whitespace and comments for nicer looking RE's.
    U  UNICODE     For compatibility only. Ignored for string patterns (it
                   is the default), and forbidden for bytes patterns.

This module also defines an exception 'error'.

"""
  • 9
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou.cs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值