网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

40python正则表达式match方法匹配字符串使

第一课: 使用match方法匹配字符串
# 正则表达式:使用match方法匹配字符串

'''
正则表达式:是用来处理文本的,将一组类似的字符串进行抽象,形成的文本模式字符串

windows dir *.txt   file1.txt  file2.txt  abc.txt  test.doc
a-file1.txt-b
linux/mac ls

主要是学会正则表达式的5方面的方法
1. match:检测字符串是否匹配正则表达式
2. search:在一个长的字符串中搜索匹配正则表达式的子字符串
3. findall:查找字符串
4. sub和subn:搜索和替换
5. split: 通过正则表达式指定分隔符,通过这个分隔符将字符串拆分
'''

import re      # 导入正则表达的模块的 

m = re.match('hello', 'hello') # 第一个指定正则表达式的字符串,第二个表示待匹配的字符串 实际上 正则表达式也可以是一个简单的字符串

print(m)    # 

print(m.__class__.__name__)  # 看一下m的类型 Match
print(type(m))                  #  

m = re.match('hello', 'world')
if m is not None:
    print('匹配成功')
else:
    print('匹配不成功')

# 待匹配的字符串的包含的话正则表达式,系统也认为是不匹配的
m = re.match('hello', 'world hello')
print(m)    # None  如果不匹配的话,返回值为None

# 待匹配的字符串的前缀可以匹配正则表达式,系统也认为是匹配的
m = re.match('hello', 'hello world')
print(m)  # 

----------------------------------------------
第二课 正则中使用search函数在一个字符串中查找子字符串

# search函数 和 match函数的区别是,search函数中 字符串存在就可以匹配到,match函数 必须要 前缀可以匹配正则表达式,系统也认为是匹配的 

import re

m = re.match('python', 'I love python.')
if m is not None:
    print(m.group())    # 调用group的方法就是返回到一个组里面
print(m)            # None
m = re.search('python', 'I love python.')
if m is not None:
    print(m.group())
print(m)  
#
# python
#   
span=(7, 13) 表示 python在 'I love python.' 字符串中的位置 
左闭右开

网站栏目:40python正则表达式match方法匹配字符串使
转载来源:http://bjjierui.cn/article/gsdhic.html

其他资讯