来源

1、python正则匹配

1.1 re.search 正则表达式

search将字符串的所有字串尝试与正则表达式匹配,如果所有的字串都没有匹配成功,返回none,否则返回matchobject;(可以理解为“无为0,有为1”)

import re
s1 = "helloworld,i am 30"
w1 = "world"
m1 = re.search(w1,s1)
if m1:
print("find:%s"%m1.group())

输出为:(m1.group()可以一次输出多个数组--对应字符串,此处匹配到一个world,则输出匹配到的一个字符串)

world

1.2 re.match() 正则表达式

match只从字符串的开始与正则表达式匹配,匹配成功返回matchobject,否则返回none

import re
s1 = "helloworld,i am 30"
w1 = "world"
m1 = re.match(w1,s1)
if m1:
print("find:%s"%m1.group())
else
print("no match")

输出为:(由于re.match()从字符串开头匹配,两个字符串都不匹配,因此无法匹配)

no match

1.3 re.match与re.search的区别

1.4 检索和替换

(1)repl参数是一个函数

(2)re.compile函数

(3)findall

在字符串中找到正则表达式所匹配的所有子串,并返回一个列表,如果没有找到匹配的,则返回空列表。

注意:match和serach是匹配一次,findall是匹配所有。

语法格式为:findall(string[, pos[, endpos]])

参数:

  • string:待匹配的字符串;
  • pos:可选参数,指定字符串的起始位置,默认为0;
  • endpos:可选参数,指定字符串的结束位置,默认为字符串的长度;

查找字符串中的所有数字:

pattern = re.complie(r'\d+')   #查找数字
result1 = pattern.findall('runoob 123 google 456')
result2 = pattern.findall('run88oobgoogle456', 0, 10) print(result1)
print(result2)

输出结果为:

['123', '456']
['88', '12']

result2 = pattern.findall('run88oobgoogle456', 0, 10)中的0指的是字符串中的起始位置,10指的是字符串的结束位置,默认为字符串的长度;

(4)re.finditer

(5)re.split

1.5 正则表达式对象

(1)re.RegexObject

(2)re.MatchObject

1.6 正则表达式修饰符 - 可选标志

1.7 正则表达式模式

1.8 正则表达式实例

1.9 正则表达式re.findall用法

语法:findall(pattern, string, flags=0)

正则re.findall是返回string中所有与pattern相匹配的全部字串,返回形式为数组;

(1)一般情况

regular_v1 = re.findall(r"docs", "https://docs.python.org/3/whatsnew/3.6.html")
print(regual_v1)

输出结果为:

['docs']

(2)符号^表示匹配https开头的字符串返回

regular_v2 = re.findall(r"^https", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v2)

输出结果为:

['https']

(3)用$符号表示以html结尾的字符串返回,判断是否字符串结束的字符串

regular_v3 = re.findall(r"html$", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v3)

输出结果为:

'html'

(4)[...]匹配括号中的其中一个字符

regular_v4 = re.findall(r"[t,w]h", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v4)

输出结果为:

['th', 'wh']

(5)“d”是正则语法规则用来匹配0到9之间的数返回列表

regular_v5 = re.findall(r"\d", "https://docs.python.org/3/whatsnew/3.6.html")
regular_v6 = re.findall(r"\d\d\d", "https://docs.python.org/3/whatsnew/3.6.html/1234")
print(regular_v5)
print(regular_v6)

输出结果为:

['3', '3', '6']
['123']

(6)小d表示取数字0-9,大D表示不要数字,也就是除了数字以外的内容返回

regular_v7 = re.findall(r"\D", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v7)

输出结果为:

['h', 't', 't', 'p', 's', ':', '/', '/', 'd', 'o', 'c', 's', '.', 'p', 'y', 't', 'h', 'o', 'n', '.', 'o', 'r', 'g', '/', '/', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '/', '.', '.', 'h', 't', 'm', 'l']

(7)“w”在正则里面代表匹配从小写a到z,大写A到Z,数字0到9

regular_v8 = re.findall(r"\w", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v8)

输出结果为:

['h', 't', 't', 'p', 's', ':', '/', '/', 'd', 'o', 'c', 's', '.', 'p', 'y', 't', 'h', 'o', 'n', '.', 'o', 'r', 'g', '/', '/', 'w', 'h', 'a', 't', 's', 'n', 'e', 'w', '/', '.', '.', 'h', 't', 'm', 'l']

(8)“W”在正则里面代表匹配除了字母与数字以外的特殊符号

regular_v8 = re.findall(r"\W", "https://docs.python.org/3/whatsnew/3.6.html")
print(regular_v8)

输出结果为:

[':', '/', '/', '.', '.', '/', '/', '/', '.', '.']

re正则表达式方法的更多相关文章

  1. 千万别把js的正则表达式方法和字符串方法搞混淆了

    我们在字符串操作过程中肯定经常用了test() split() replace() match() indexof()等方法,很多人经常把用法写错了,包括我,所以今天细细的整理了下. test()是判 ...

  2. JS正则表达式方法

    使用正则表达式的主要有match,exec,test 1.正则表达式方法test测试给定的字符串是否满足正则表达式,返回值是bool类型的,只有真和假. var user_code = $(" ...

  3. 【六】PHP正则表达式方法

    PHP中正则表达式的声明格式有两种方式,一种是POSIX老版模式,已经不常用.还有一种是其他语言中常见的PCRE方法. 1.正则表达式的匹配方法并返回匹配的项:array preg_grep(stri ...

  4. JavaScript正则表达式方法总结

    str.match(reg) 1.reg没有全局标志g,match将只执行一次匹配.匹配成功返回一个数组,arr = [$0,$1,$2,...,index,str],匹配失败返回null. arr中 ...

  5. js 去掉前后空格(正则表达式方法)

    代码: ' aaa '.replace(/(^\s*)|(\s*$)/g, '')

  6. 浅析正则表达式模式匹配的String方法

    在JavaScript代码中使用正则表达式进行模式匹配经常会用到String对象和RegExp对象的一些方法,例如replace.match.search等方法,以下是对一些方法使用的总结. Stri ...

  7. js中正则表达式 书写方法

    function test(){    var text="index.aspx?test=1&ww=2&www=3";            var   re = ...

  8. 浅析正则表达式模式匹配的 String 方法

    在JavaScript代码中使用正则表达式进行模式匹配经常会用到String对象和RegExp对象的一些方法,例如replace.match.search等方法,以下是对一些方法使用的总结. Stri ...

  9. JavaScript 正则表达式——定义,目的,特点,语法,字符串方法,search() ,replace() ,test(),exec()

    ㈠什么是正则表达式? ⑴正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.    正则表达式通常被用来检 ...

随机推荐

  1. fedora23安装搜狗輸入法?

    1, 安裝方法, 是通過下載 repo文件, 添加repo文件, 然後通過dnf啦安裝的. repo文件地址是: fedora 的中文社區: https://www.fdzh.org/ fdzh: 就 ...

  2. luogu P4051 [JSOI2007]字符加密

    前言 其实就是个后缀数组模板题 可还是有几个的地方不太明白 思路 先将子串复制一遍,组成长度为2*n的子串 给出的子串一定会在前n个后缀 而且后面的优先级不会影响前面的相对大小 然后求得sa输出就好 ...

  3. RocketMQ3.2.2生产者发送消息自动创建Topic队列数无法超过4个

    问题现象 RocketMQ3.2.2版本,测试时尝试发送消息时自动创建Topic,设置了队列数量为8: producer.setDefaultTopicQueueNums(8); 同时设置broker ...

  4. html 之 body topmargin、leftmargin、rightmargin、bottomnargin

    基本语法 <body topmargin=value leftmargin=value rightmargin=value bottomnargin=value> 语法说明 通过设置top ...

  5. .Net Core 全球化&本地化的使用

    官网文档 nuget地址 创建资源文件 添加资源文件 实施策略 配置本地化 本地化中间件 使用 视图本地化 DataAnnotations 本地化 Make the app's content loc ...

  6. Lintcode449-Char to Integer-Naive

    Description Convert a char to an integer. You can assume the char is in ASCII code (See Definition, ...

  7. 定义统一的返回格式(controller)

    一:单独创建一个类来表示返回结果 package com.jk51.commons.dto; /** * Created by Administrator on 2017/6/13. */ publi ...

  8. Linux let 命令

    命令:let let 命令是 BASH 中用于计算的工具,用于执行一个或多个表达式,变量计算中不需要加上 $ 来表示变量.如果表达式中包含了空格或其他特殊字符,则必须引起来. 语法格式 let arg ...

  9. 关于js中splice方法返回的结果

    一.前言 刚刚在使用splice()方法,发现这个方法返回的是删除后的数组元素,如果要获取删除指定元素后的数组,直接调用原来的数组即可!因为splice()会改变原来数组!之前对splice()方法一 ...

  10. Intellij idea 添加浏览器

    最近的项目要做一个海康的网页端的监控,需要下载海康的插件,但是试验了一下,Chrome和IE的都不支持插件的显示,只有搜狗的显示,但是Idea的默认浏览器里面没有,所以要添加一个默认的浏览器 方法很简 ...