python26 re正则表达式
#coding:utf-8
#/usr/bin/python
"""
2018-11-25
dinghanhua
re
"""
import re
teststr = '"id":"2994925","publisher":"Yahoo Press","isbn10":"0596517742","isbn13":"9780596517748","title":"JavaScript","url":"https:\/\/api.douban.com\/v2\/book\/2994925","alt_title":"","author_intro":"Douglas Crockford is a Senior JavaScript Architect at Yahoo!. He is the maintainer of the JSON format, and a regular speaker at conferences on advanced JavaScript topic. He is also on the JavaScript 2.0 committee at ECMA."'
'''re.match() 从字符串的起始位置匹配 '''
pattern = r'\d+'
print(re.match(pattern,teststr))
pattern = r'"id":"(.*)","publisher'
matchobj = re.match(pattern,teststr)
print(matchobj.group(0))
print(matchobj.groups())
print(matchobj.group(1))
print(matchobj.span())
print(matchobj.start(),matchobj.end())
'''re.search() 返回字符串中第一个匹配的 '''
pattern = r'\d+'
print(re.search(pattern,teststr))
pattern = r'"id":"(.*?)".*"title":"(.*?)"'
matchobj = re.search(pattern,teststr)
print(matchobj.group(0))
print(matchobj.groups())
print(matchobj.group(1,2))
print(matchobj.span())
print(matchobj.start(),matchobj.end())
'''re.sub() 替换匹配项 repl=替换的字符串,count替换几个,默认0替换所有'''
pattern = r'\d+'
teststr2 = re.sub(pattern,repl='',string=teststr,count=1)
print(teststr2) pattern = r'\D+'
teststr2 = re.sub(pattern,"",teststr) #去掉所有非数字
print(teststr2)
'''compile()生成正则表达式对象'''
pattern = re.compile(r'"(\w+)":"(\w+)"')
matchobj = pattern.match(teststr)
print(matchobj.groups())
matchobj = pattern.search(teststr,10,100) #设定起始结束位置
print(matchobj.groups())
'''findall 匹配所有,返回列表'''
pattern = r'"(\w+)":"(\d+)"'
matchlist = re.findall(pattern,teststr)
print(matchlist)
pattern = re.compile(r'"(\w+)":"(\D+)"')
matchlist = pattern.findall(teststr,10)
print(matchlist)
'''re.finditer 匹配所有,返回迭代器'''
pattern = r'"(\w+)":"(\d+)"'
matchiter = re.finditer(pattern,teststr)
print(matchiter)
for m in matchiter:
print(m.groups())
'''re.split() 正则分隔'''
pattern = r'[^a-zA-Z]+' #根据非字母分隔
splitlist = re.split(pattern,teststr)
print(splitlist)
python26 re正则表达式的更多相关文章
- python的OS库和正则表达式库
摘自:http://blog.chinaunix.net/uid-16360955-id-3351990.html 作留存学习 1.常用内置函数:(不用import就可以直接使用) help(obj) ...
- JS正则表达式常用总结
正则表达式的创建 JS正则表达式的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\\s+) ...
- Python高手之路【五】python基础之正则表达式
下图列出了Python支持的正则表达式元字符和语法: 字符点:匹配任意一个字符 import re st = 'python' result = re.findall('p.t',st) print( ...
- C# 正则表达式大全
文章导读 正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET提供的Regex类实现了验证正则表达式的方法.Regex 类表示不可变(只读)的 ...
- C#基础篇 - 正则表达式入门
1.基本概念 正则表达式(Regular Expression)就是用事先定义好的一些特定字符(元字符)或普通字符.及这些字符的组合,组成一个“规则字符串”,这个“规则字符串”用来判断我们给定的字符串 ...
- JavaScript正则表达式,你真的知道?
一.前言 粗浅的编写正则表达式,是造成性能瓶颈的主要原因.如下: var reg1 = /(A+A+)+B/; var reg2 = /AA+B/; 上述两个正则表达式,匹配效果是一样的,但是,效率就 ...
- Python 正则表达式入门(中级篇)
Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...
- 【JS基础】正则表达式
正则表达式的() [] {}有不同的意思. () 是为了提取匹配的字符串.表达式中有几个()就有几个相应的匹配字符串. (\s*)表示连续空格的字符串. []是定义匹配的字符范围.比如 [a-zA-Z ...
- JavaScript 正则表达式语法
定义 JavaScript定义正则表达式有两种方法. 1.RegExp构造函数 var pattern = new RegExp("[bc]at","i"); ...
随机推荐
- vue 浏览器顶部有载入(进度)动画插件vue-progressbar
1.安装 npm install --save nprogress 2.在main.js中引入 import NProgress from "nprogress" import & ...
- GreenPlum 大数据平台--介绍
一,GreenPlum 01,介绍: Greenplum是一种基于PostgreSQL的分布式数据库,其采用shared-nothing架构,主机.操作系统.内存.存储都是自我控制的,不存在共享. 官 ...
- HTML练习 | 百度搜索框
<!DOCTYPE html> <head> <title>百度首页</title> <style> .logo{ background:u ...
- Oracle 角色及其权限
一.简介 Oracle权限分为系统权限和对象权限. 1.系统权限 注意:系统权限不支持级联回收,所以你需要使用sysdba一个个的回收. 2.对象权限 注:对象权限支持级联回收,系统权限不支持级联回收 ...
- Jenkins+Postman+Newma+Xmysql之API全自动化测试
第一章 前期准备:各种安装配置介绍 ①Postman安装及使用 ②Newman 安装及使用 ③Xmysql 安装及使用 ④Jenkins安装及配置 1.postman 安装及使用 1.1.postma ...
- 基于 CentOS 搭建 WordPress 个人博客
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 腾讯云提供了开发者实验室帮助用户搭建 WordPress 个人博客,教程内容如下,用户可以点击开发者实验室快速上机完成实验. 准备 LNMP ...
- mysql用户操作
一, 创建用户: 命令:CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username - 你将创建的用户名, host - 指 ...
- FZU 1922——非主流——————【技巧题】
非主流 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- 整理代码,将一些曾经用过的功能整合进一个spring-boot
一 由于本人的码云太多太乱了,于是决定一个一个的整合到一个springboot项目里面. 附上自己的项目地址https://github.com/247292980/spring-boot 功能 1. ...
- Javascript 5种设计风格
1.过程式的程序设计 <script> /*Start and Stop animations using functions.*/ function startAnimation() { ...