import re pattern = "this" text = "Does this text match the pattern?" match = re.search(pattern, text) # 返回一个Match对象 print match.re.pattern # 要匹配的正则表达式"this" print match.string # 匹配的文本"Does this match the pattern?" print match.start() # 匹配的开始位置 5 print match.end() # 匹配的结束位置 9
import re
regex = re.compile("this")
text = "Does this text match the pattern?"
match = regex.search(text)
if match:
print "match"
match.group(0) #返回匹配的字符串
else:
print "not match"
import re pattern = 'ab' text = 'abbaaabbbbaaaaaa' re.findall(pattern, text) # 返回['ab', 'ab']
import re pattern = 'ab' text = 'abbaaabbbbaaaaaa' match = re.finditer(pattern, text) for m in match: print m.start() print m.end()
import re
re.search("^travell?er$", "traveler") # True
re.search("^travell?er$", "traveller") # True
re.search("^ab\*", "abbbbbbb") # True,返回"abbbbbbb"
re.search("^ab\*?", "abbbbbbb") # True,返回"a"
re.search("^ab+", "abbbbbbb") # True,返回"abbbbbbb"
re.search("^ab+?", "abbbbbbb") # True,返回"ab"
import re the_str = "This is some text -- with punctuation" re.search(r'^\w+', the_str).group(0) # This re.search(r'\A\w+', the_str).group(0) # This re.search(r'\w+\S*$', the_str).group(0) # punctuation re.search(r'\w+\S*\Z', the_str).group(0) # punctuation re.search(r'\w*t\W*', the_str).group(0) # text -- re.search(r'\bt\w+', the_str).group(0) # text re.search(r'\Bt*\B', the_str).group(0) # 没有匹配
import re
the_str = "--aabb123bbaa"
pattern = r'(\W+)([a-z]+)(\d+)(\D+)'
match = re.search(pattern, the_str)
match.groups() # ('--', 'aabb', '123', 'bbaa')
match.group(0) # '--aabb123bbaa'
match.group(1) # '--'
match.group(2) # 'aabb'
match.group(3) # '123'
match.group(4) # 'bbaa'
import re
the_str = "--aabb123bbaa"
pattern = r'(?P<not_al_and_num>\W+)(?P<al>[a-z]+)(?P<num>\d+)(?P<not_num>\D+)'
match = re.search(pattern, the_str)
match.groups() # ('--', 'aabb', '123', 'bbaa')
match.groupdict() # {'not_al_and_num': '--', 'not_num': 'bbaa', 'num': '123', 'al': 'aabb'}
match.group(0) # '--aabb123bbaa'
match.group(1) # '--'
match.group(2) # 'aabb'
match.group(3) # '123'
match.group(4) # 'bbaa'
match.group('not_al_and_num') # '--'
match.group('al') # 'aabb'
match.group('num') # '123' '
match.group('not_num') # 'bbaa'
import re the_str = "this Text" re.findall(r'\bt\w+', the_str) # ['this'] re.findall(r'\bt\w+', the_str, re.IGNORECASE) # ['this', 'Text']
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有