#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正则表达式的更多相关文章

  1. python的OS库和正则表达式库

    摘自:http://blog.chinaunix.net/uid-16360955-id-3351990.html 作留存学习 1.常用内置函数:(不用import就可以直接使用) help(obj) ...

  2. JS正则表达式常用总结

    正则表达式的创建 JS正则表达式的创建有两种方式: new RegExp() 和 直接字面量. //使用RegExp对象创建 var regObj = new RegExp("(^\\s+) ...

  3. Python高手之路【五】python基础之正则表达式

    下图列出了Python支持的正则表达式元字符和语法: 字符点:匹配任意一个字符 import re st = 'python' result = re.findall('p.t',st) print( ...

  4. C# 正则表达式大全

    文章导读 正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET提供的Regex类实现了验证正则表达式的方法.Regex 类表示不可变(只读)的 ...

  5. C#基础篇 - 正则表达式入门

    1.基本概念 正则表达式(Regular Expression)就是用事先定义好的一些特定字符(元字符)或普通字符.及这些字符的组合,组成一个“规则字符串”,这个“规则字符串”用来判断我们给定的字符串 ...

  6. JavaScript正则表达式,你真的知道?

    一.前言 粗浅的编写正则表达式,是造成性能瓶颈的主要原因.如下: var reg1 = /(A+A+)+B/; var reg2 = /AA+B/; 上述两个正则表达式,匹配效果是一样的,但是,效率就 ...

  7. Python 正则表达式入门(中级篇)

    Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...

  8. 【JS基础】正则表达式

    正则表达式的() [] {}有不同的意思. () 是为了提取匹配的字符串.表达式中有几个()就有几个相应的匹配字符串. (\s*)表示连续空格的字符串. []是定义匹配的字符范围.比如 [a-zA-Z ...

  9. JavaScript 正则表达式语法

    定义 JavaScript定义正则表达式有两种方法. 1.RegExp构造函数 var pattern = new RegExp("[bc]at","i"); ...

随机推荐

  1. 马踏棋盘--dfs

    [问题描述]关于马踏棋盘的基本过程:国际象棋的棋盘为 8*8 的方格棋盘.现将"马"放在任意指定的方格中,按照"马"走棋的规则将"马"进行移 ...

  2. 【记录】SQL注入过滤源码分享

    $id=check_addslashes($_GET['id']);$id= preg_replace('/o*r/i',"", $id); //strip out OR (non ...

  3. C#中Using里使用单例的问题

    又给自己挖了一个坑跳进去. KafkaManager使用单例模型获取到一个producer,然而自己代码里用的时候加了一个using using (var producer = KafkaManage ...

  4. win 引用win32无效问题

    我使用pycharm 安装: python window 10 下安装win32 module 问题:安装后引用win32api等依赖,无效,但是目录下确实已经安装: 解决方案: ${python_h ...

  5. Python学习 day07

    一.关于解决问题的思路 1.删除列表中索引为单数的元素. 别人的思路: 利用切片 li = [11, 22, 33, 44, 55] li = li[::2] print(li) 思考:虽然学了pyt ...

  6. pg存储过程和sql语句块

    展E宝项目使用的是postgresql数据库,批量发送红包需求,需要采用存储过程来初始化红包记录数据. 创建存储过程语句有固定的架子,如下 CREATE OR REPLACE FUNCTION pub ...

  7. layer弹出层显示在top顶层

    父页面 导入 layer.js 或者 layui.all.js,导入后就能正常显示在父窗口页面区域. 1.显示在顶层窗口 top.layer.open({ type: 2, area: ['98%', ...

  8. curl -w函数

    url_effective 最终获取的url地址,尤其是当你指定给curl的地址存在301跳转,且通过-L继续追踪的情形. http_code http状态码,如200成功,301转向,404未找到, ...

  9. hibernate框架配置文件详解

    1 orm元数据配置文件(映射文件) <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hib ...

  10. 在windows上用netsh动态配置端口转发

    使用多个虚拟机,将开发环境和工作沟通环境分开(即时通,办公系统都只能在windows下使用…),将开发环境的服务提供给外部访问时,需要在主机上通过代理配置数据转发. VirtualBox提供了端口转发 ...