Python正则表达式中的 compile,search,group,groups 函数的简单说明
2016-06-29 15:30
645 查看
Python正则表达式中的 compile,search,group,groups 函数的简单说明
2013年12月04日 ⁄ 综合 ⁄ 共 306字 ⁄ 字号 小 中 大 ⁄ 评论关闭<iframe id="iframeu1788635_0" src="http://pos.baidu.com/jcsm?rdid=1788635&dc=2&di=u1788635&dri=0&dis=0&dai=2&ps=236x1186&dcb=BAIDU_SSP_define&dtm=BAIDU_DUP_SETJSONADSLOT&dvi=0.0&dci=-1&dpt=none&tsr=0&tpr=1467184976401&ti=Python%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E4%B8%AD%E7%9A%84%20compile%2Csearch%2Cgroup%2Cgroups%20%E5%87%BD%E6%95%B0%E7%9A%84%E7%AE%80%E5%8D%95%E8%AF%B4%E6%98%8E%20%7C%20%E5%AD%A6%E6%AD%A5%E5%9B%AD&ari=1&dbv=2&drs=1&pcs=1920x951&pss=1920x256&cfv=0&cpl=5&chi=1&cce=true&cec=UTF-8&tlm=1467184976&rw=951<u=http%3A%2F%2Fwww.xuebuyuan.com%2F1537257.html<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DaaaHCH3u7r9fXco4zM4XsXI2KahntPv1RSIgpVetbomxPI6ajFWN6xXpIKVpc3KL%26wd%3D%26eqid%3Dac43dd07000636af0000000357737746&ecd=1&psr=1920x1080&par=1920x1080&pis=-1x-1&ccd=24&cja=false&cmi=7&col=zh-CN&cdo=-1&tcn=1467184977&qn=b71d7763a4d1d7a1&tt=1467184976361.159.359.361" width="336" height="280" align="center,center" vspace="0" hspace="0" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="margin: 0px; padding: 0px; border-width: 0px; border-style: initial; vertical-align: bottom; background: transparent;"></iframe>
以此为例:regex = re.compile('\((.*)\)')
里面嵌套的那对括号是用于识别group的,所以至多有group(1);
compile是编译正则表达式,生成pattern对象;
pattern.search(S)就是在字符串S中寻找匹配之前生成pattern的子串;
而group和groups是两个不同的函数:
一般,m.group(N) 返回第N组括号匹配的字符。
而m.group() == m.group(0) == 所有匹配的字符,与括号无关,这个是API规定的。
m.groups() 返回所有括号匹配的字符,以tuple格式。
m.groups() == (m.group(1), m.group(2), ...)
在 http://docs.python.org/library/re.htm...
看到
m = re.match(r"(..)+", "a1b2c3") # Matches 3 times. m.group(1) # Returns only the last match. 'c3' m.group(0) 'a1b2c3' m.groups() ('c3',)
注意到pattern中的+,应该是匹配偶数个字符.
1.首先是match的问题.match是从开头匹配,为什么会匹配到c3呢?
2.group(0)是整个匹配项,为什么groups()中没有呢?
2011年09月11日提问
评论
邀请回答
默认排序时间排序
3 个回答
答案对人有帮助,有参考价值1
答案没帮助,是错误的答案,答非所问
采纳
1) 从group的角度考虑,整个表达式应该写作((..)+)。所以最高一层group(0)匹配整个字符串。
2) 先说第二个问题,按照文档的描述的行为,group()是列出从编号1开始的所有group,而给定的表达式只有一个group,所以自然就是c3了
3) 好,最后是为什么group(1)是c3。如果你的正则表达式没有那个“+”,那么它就只匹配a1。而有了“+”之后,每匹配到一个“(..)”就会放到group(1)中。所以跑完整个字符串,group(1)就存进了c3
我猜你其实想问“字符串中明明有三个符合(..)的,但为什么只出现了最后一个呢?”原因是,group说的是正则表达式中的括号,而不是字符串中符合括号内pattern的子串。
encode 函数
def py_encode_basestring_ascii(s):
"""Return an ASCII-only JSON representation of a Python string
"""
if isinstance(s, str) and HAS_UTF8.search(s) is not None:
s = s.decode('utf-8')
def replace(match):
s = match.group(0)
try:
return ESCAPE_DCT[s]
except KeyError:
n = ord(s)
if n < 0x10000:
return '\\u{0:04x}'.format(n)
#return '\\u%04x' % (n,)
else:
# surrogate pair
n -= 0x10000
s1 = 0xd800 | ((n >> 10) & 0x3ff)
s2 = 0xdc00 | (n & 0x3ff)
return '\\u{0:04x}\\u{1:04x}'.format(s1, s2)
#return '\\u%04x\\u%04x' % (s1, s2)
return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"'
可见输出json_dumps(ensure_asscii=false)输出的是utf-16形式的字符串
python 中
类似 直接先转换成utf-16 全部显示成\\u格式 (包括<=127)区别php
php中(json.c)文件
首先转换为utf-16编码 unsigned short *a[]
如果某个元素<=127 则直接append到output中
否则 一律\\u..输出 意味着 utf-16字符集存储编码中 单个word可能输出\u 或者 asscii字符,两个word(110110 110111后跟10位)一定大于127 必然输出 \\u格式
相关文章推荐
- python django下载大的csv文件
- python画图包seaborn和matplotlib中文字体显示(针对windows系统,,使用ipython notebook)
- Python之路_Day8
- python笔记 - day3
- 支持向量机 smo算法
- Python学习-集合
- PyCharm设置python新建文件 模板修改 设置 文件为python 和utf-8 解决控制台输出乱码
- python笔记
- Python对中文字符的处理(utf-8/ gbk/ unicode)
- [python] itertools库学习
- python头部注释 vim添加头部注释
- 用Python买双色球,赢取1千万!
- python异步回调函数的实现
- 常见递归问题 Python解法
- python实例-暂停一秒
- python2.0_s12_day11_SqlAlchemy使用介绍
- Python模块之Logging(五)——在配置文件中为Logger配置多个handler
- Python实战1_3:爬取租房信息
- Python 核心编程笔记_Chapter_3 Note_4 创建python文件(练习文件操作)
- Python filter map reduce lambda Method detail