python 遍历函数用法_python遍历循环python正则表达式函数match()和search()的区别详解...

match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢?

match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找匹配, 也就是说match()只有在0位置匹配成功的话才有返回,如果不是开始位置匹配成功的话,match()就返回none

例如:#! /usr/bin/env python

# -*- coding=utf-8 -*-

import re

text = 'pythontab'

m = re.match(r"\w+", text)

if m:

print m.group(0)

else:

print 'not match'

结果是:pythontab

而:#! /usr/bin/env python

# -*- coding=utf-8 -*-

#

import re

text = '@pythontab'

m = re.match(r"\w+", text)

if m:

print m.group(0)

else:

print 'not match'

结果是:not match

search()会扫描整个字符串并返回第一个成功的匹配

第二电脑网认为此文章对《python遍历循环python正则表达式函数match()和search()的区别详解》说的很在理,电脑技术002pc网为你提供最佳的python编码,python培训。

例如:#! /usr/bin/env python

# -*- coding=utf-8 -*-

#

import re

text = 'pythontab'

m = re.search(r"\w+", text)

if m:

print m.group(0)

else:

print 'not match'

结果是:pythontab

那这样呢:#! /usr/bin/env python

# -*- coding=utf-8 -*-

#

import re

text = '@pythontab'

m = re.search(r"\w+", text)

if m:

print m.group(0)

else:

print 'not match'

结果是:pythontab

更多:python遍历循环python正则表达式函数match()和search()的区别详解

https://www.002pc.comhttps://www.002pc.com/python/3710.html

你可能感兴趣的search,python,match,详解,正则表达式,函数

No alive nodes found in your cluster

0踩

0 赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值