易截截图软件、单文件、免安装、纯绿色、仅160KB

Python正则表达式的常用匹配用法


下面列出Python正则表达式的几种匹配用法:
1.测试正则表达式是否匹配字符串的全部或部分
regex=ur"" #正则表达式
if re.search(regex, subject):
    do_something()
else:
    do_anotherthing()
2.测试正则表达式是否匹配整个字符串
regex=ur"\Z" #正则表达式末尾以\Z结束
if re.match(regex, subject):
    do_something()
else:
    do_anotherthing()
3.创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    # match start: match.start()
    # match end (exclusive): atch.end()
    # matched text: match.group()
    do_something()
else:
    do_anotherthing()
4.获取正则表达式所匹配的子串(Get the part of a string matched by the regex)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group()
else:
    result = ""
5. 获取捕获组所匹配的子串(Get the part of a string matched by a capturing group)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group(1)
else:
    result = ""
6. 获取有名组所匹配的子串(Get the part of a string matched by a named group)
regex=ur"" #正则表达式
match = re.search(regex, subject)
if match:
    result = match.group"groupname")
else:
    result = ""
7. 将字符串中所有匹配的子串放入数组中(Get an array of all regex matches in a string)
result = re.findall(regex, subject)
8.遍历所有匹配的子串(Iterate over all matches in a string)
for match in re.finditer(r"<(.*?)\s*.*?/\1


相关文档:

Python的lambda函数与排序

前几天看到了一行求1000的阶乘的Python代码:
print
  
reduce
(
lambda
  
x
,
y
:
x
*
y
,
  
range
(
1
,
  
1001
))

一下子被python代码的精简
与紧凑所折服,故对代码进行了简单的分析。
reduce与range都是Python的内置函数。
range(1,10 ......

Python中Range和XRange的区别

Python中Range和XRange的区别(Difference between Range and XRange in Python)
最近机器出了点问题,所以一直没有写新的东西出来。之前在读Python的代码的时候,发觉好多人喜欢用XRange,而不是Range,我也只是记得学习的时候开始学到的是Range,后面又看到有个XRange,当时也没有深究,为什么Python里面要有两个同样的功 ......

python安装之安装模块制作

python的egg文件有点像java中的jar文件,是一个工程打包文件,便于安装部署,仅此一点,给多少pythoner带来了多少激动。
如何制作egg文件呢?see官方文档http://peak.telecommunity.com/DevCenter/PythonEggs,
到http://pypi.python.org/pypi/setuptools下载setuptools包,然后安装:
python setup.py
1.制作egg文件
......

python 内置数据类型

▾ hide table of contents
0. ↑ 显示完整目录
1. 深入#
2. 布尔类型#
3. 数值类型#
1. 将整数强制转换为浮点数及反向转换#
2. 常见数值运算#
3. 分数#
4. 三角函数#
5. 布尔上下文环境中的数值#
4. 列表#
1. 创建列表#
......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号