python学习基础篇——正则表达式常用函数

正则表达式常用函数

dir(re)  help(re.function_name)

  • 正则表达式编译 re.compile()  -- return a sre.Pattern object
  • re.findall() :
    • findall(pattern, string, flags=0)
    • Return a list of all non-overlapping matches in the string.
  • match() -- 决定RE 是否在字符串刚开始的位置匹配,没有匹配返回None 成功的话返回 "MatchObject"
    • MatchObject.group() 返回匹配开始的位置
    • MatchObject.end() 返回匹配结束的位置
    • MatchObject.span() 返回一个元组 包含匹配(开始,结束)的位置。
  •  search()  -- 扫描字符串 找到这个RE匹配的位置,没有匹配返回None 成功的话返回 "MatchObject"实例
  • findall() -- 找到RE匹配的所有字符串,并把他们作为一个列表返回,没有匹配返回[], 成功的话返回 列表
  •  finditer() -- 找到RE匹配的所有字符串,并把它们作为一个迭代器返回

 模块级函数

match() search() sub() subn() spilt() findall()等

  • sub() -- 用于替换字符串中符合正则表达式的内容,返回替换后的字符串
    •  help(re.sub)-- sub(pattern, repl, string, count=0, flags=0) 正则 替换 原始字符串 最大替换次数
  • subn() -- 与sub()函数效果相同 只是返回的是被替换的次数
  • split() -- 用于分割字符串
    •   split(pattern, string, maxsplit=0, flags=0)
    • Split the source string by the occurrences of the pattern,returning a list containing the resulting substrings.
import re
# ……………………………………………………  match  ………………………………………………………………
# match 只匹配开头是否符合规则 
python_re = re.compile(r"python",re.I)
one = python_re.match("python hello !")
two = python_re.match("hello python !")
print(one) # <_sre.SRE_Match object; span=(0, 6), match='python'>
print(two)  # None
print(one.group()) # python返回被RE匹配的字符串
print(one.start()) # 0 
print(one.end()) # 6 
print(one.span()) # (0,6) 
# 通常使用match 来判断匹配的数据是否成功
if one:
	print("Match Found:",one.group())
else:
	print("No match") # Match Found: python
# ……………………………………………………  search  ………………………………………………………………
# search : 只要字符串存在就能匹配到,且只匹配第一个符合规则的字符串
print(python_re.search("hello python !"))  # <_sre.SRE_Match object; span=(6, 12), match='python'>
print(python_re.search("python hello !")) # <_sre.SRE_Match object; span=(0, 6), match='python'>
print(python_re.search("hello !")) # None
print(python_re.search("hello python python !"))
# ……………………………………………………  finditer  ………………………………………………………………
# finditer : 返回的是迭代器 需要使用迭代的形式才能把字符串显示处理
print(python_re.finditer("python hello python hello !")) # <callable_iterator object at 0x00000000028EB240>
x = python_re.finditer("python hello python hello !")
print("````````````````")
# next() 输出迭代器的下一个元素
print(next(x)) # <_sre.SRE_Match object; span=(0, 6), match='python'> 返回一个match函数结果相同含义的对象

# match 下的子函数
# group
print(next(x).group()) # python

# ……………………………………………………  模块级函数  ………………………………………………………………
# match() search() sub() subn() spilt() findall()等 

# sub()
str = "hello Python , python is so easy, love python."
print(str.replace("python","Java")) # 指定字符串实现字符串间的替换。
# hello Python , Java is so easy, love Java.

print(re.sub(python_re,"Java",str)) # 替换符合正则表达式规则的字符串 更好用。
# hello Java , Java is so easy, love Java.

# subn()
print(re.subn(python_re,"Java",str)) 
# ('hello Java , Java is so easy, love Java.', 3)

# spilt
ip = "1.2.3.4"
print(ip.split(".")) # ['1', '2', '3', '4']
s= "123+23*45-3672/23"
# +-*/ 元字符有特殊含义 使用转义字符\ 
print(re.split(r"[\+\-\*\/]",s)) # ['123', '23', '45', '3672', '23']

print(dir(re)) # 查看re 下所有的模块函数 用于学习使用 查看方法使用help()函数。
# ['A', 'ASCII', 'DEBUG', 'DOTALL', 'I', 'IGNORECASE', 'L', 'LOCALE', 
# 'M', 'MULTILINE', 'RegexFlag', 'S', 'Scanner', 'T', 'TEMPLATE', 'U', 'UNICODE', 
# 'VERBOSE', 'X', '_MAXCACHE', '__all__', '__builtins__', '__cached__', '__doc__', 
# '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', 
# '_alphanum_bytes', '_alphanum_str', '_cache', '_compile', '_compile_repl', '_expand',
# '_locale', '_pattern_type', '_pickle', '_subx', 'compile', 'copyreg', 'enum', 'error', 
# 'escape', 'findall', 'finditer', 'fullmatch', 'functools', 'match', 'purge', 'search', 
# 'split', 'sre_compile', 'sre_parse', 'sub', 'subn', 'template']
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值