正则表达式中match、search、split、findall用法

正则表达式常用的方法:
match
用法介绍:
Try to apply the pattern at the start of the string, returning
a Match object, or None if no match was found:

import re
result='hello world,哈喽'
ret=re.match('hello',result)
print(ret.group())
**output**
hello

seach
用法介绍:
“”“Scan through string looking for a match to the pattern, returninga Match object, or None if no match was found.”""

import re
result='hello world,哈喽'
ret=re.search('world',result)
print(ret.group())
**output**
world

split
用法介绍:
“”“Split the source string by the occurrences of the pattern,
returning a list containing the resulting substrings. If
capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resultinglist. If maxsplit is nonzero, at most maxsplit splits occur,and the remainder of the string is returned as the final elementof the list.”""

import re
result='hello world,哈喽'
ret=re.split(' |,',result)
print(ret)
**output**
['hello', 'world', '哈喽']

findall
用法介绍:
“”“Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, returna list of groups; this will be a list of tuples if the patternhas more than one group.Empty matches are included in the result.”""

import re
result='hello world,哈喽'
ret=re.findall('world|哈喽',result)
print(ret)
**output**
['world', '哈喽']

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值