def match(pattern, string, flags=0): """Try to apply the pattern at the start of the string, returning a match object, or None if no match was found.""" return _compile(pattern, flags).match(string)
import re
string = "abcdef"
m = re.match("abc",string) (1)匹配"abc",并查看返回的结果是什么
print(m)
print(m.group())
n = re.match("abcf",string)
print(n) (2)字符串不在列表中查找的情况
l = re.match("bcd",string) (3)字符串在列表中间查找情况
print(l)
<_sre.SRE_Match object; span=(0, 3), match='abc'> (1)abc (2) None (3) None (4)
def fullmatch(pattern, string, flags=0): """Try to apply the pattern to all of the string, returning a match object, or None if no match was found.""" return _compile(pattern, flags).fullmatch(string)
def search(pattern, string, flags=0): """Scan through string looking for a match to the pattern, returning a match object, or None if no match was found.""" return _compile(pattern, flags).search(string) search(pattern,string,flags)的注释是Scan throgh string looking for a match to the pattern,returning a match object,or None if no match was found.在字符串任意一个位置查找正则表达式,如果找到了则返回match object对象,如果查找不到则返回None。
import re
string = "ddafsadadfadfafdafdadfasfdafafda"
m = re.search("a",string) (1)从中间开始匹配
print(m)
print(m.group())
n = re.search("N",string) (2)匹配不到的情况
print(n)
<_sre.SRE_Match object; span=(2, 3), match='a'> (1)a (2)None (3)
def sub(pattern, repl, string, count=0, flags=0):
"""Return the string obtained by replacing the leftmost
non-overlapping occurrences of the pattern in string by the
replacement repl. repl can be either a string or a callable;
if a string, backslash escapes in it are processed. If it is
a callable, it's passed the match object and must return
a replacement string to be used."""
return _compile(pattern, flags).sub(repl, string, count)
sub(pattern,repl,string,count=0,flags=0)查找替换,就是先查找pattern是否在字符串string中;repl是要把pattern匹配的对象,就要把正则表达式找到的字符替换为什么;count可以指定匹配个数,匹配多少个。示例如下:
import re
string = "ddafsadadfadfafdafdadfasfdafafda"
m = re.sub("a","A",string) #不指定替换个数(1)
print(m)
n = re.sub("a","A",string,2) #指定替换个数(2)
print(n)
l = re.sub("F","B",string) #匹配不到的情况(3)
print(l)
def subn(pattern, repl, string, count=0, flags=0): """Return a 2-tuple containing (new_string, number). new_string is the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in the source string by the replacement repl. number is the number of substitutions that were made. repl can be either a string or a callable; if a string, backslash escapes in it are processed. If it is a callable, it's passed the match object and must return a replacement string to be used.""" return _compile(pattern, flags).subn(repl, string, count)
import re
string = "ddafsadadfadfafdafdadfasfdafafda"
m = re.subn("a","A",string) #全部替换的情况 (1)
print(m)
n = re.subn("a","A",string,3) #替换部分 (2)
print(n)
l = re.subn("F","A",string) #指定替换的字符串不存在 (3)
print(l)
def split(pattern, string, maxsplit=0, flags=0):
"""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 resulting
list. If maxsplit is nonzero, at most maxsplit splits occur,
and the remainder of the string is returned as the final element
of the list."""
return _compile(pattern, flags).split(string, maxsplit)
split(pattern,string,maxsplit=0,flags=0)是字符串的分割,按照某个正则要求pattern分割字符串,返回一个列表returning a list containing the resulting substrings.就是按照某种方式分割字符串,并把字符串放在一个列表中。实例如下:
import re
string = "ddafsadadfadfafdafdadfasfdafafda"
m = re.split("a",string) #分割字符串(1)
print(m)
n = re.split("a",string,3) #指定分割次数
print(n)
l = re.split("F",string) #分割字符串不存在列表中
print(l)
['dd', 'fs', 'd', 'df', 'df', 'fd', 'fd', 'df', 'sfd', 'f', 'fd', ''] (1) ['dd', 'fs', 'd', 'dfadfafdafdadfasfdafafda'] (2) ['ddafsadadfadfafdafdadfasfdafafda'] (3)
def findall(pattern, string, flags=0):
"""Return a list of all non-overlapping matches in the string.
If one or more capturing groups are present in the pattern, return
a list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result."""
return _compile(pattern, flags).findall(string)
findall(pattern,string,flags=)是返回一个列表,包含所有匹配的元素。存放在一个列表中。示例如下:
import re
string = "dd12a32d46465fad1648fa1564fda127fd11ad30fa02sfd58afafda"
m = re.findall("[a-z]",string) #匹配字母,匹配所有的字母,返回一个列表(1)
print(m)
n = re.findall("[0-9]",string) #匹配所有的数字,返回一个列表 (2)
print(n)
l = re.findall("[ABC]",string) #匹配不到的情况 (3)
print(l)
['d', 'd', 'a', 'd', 'f', 'a', 'd', 'f', 'a', 'f', 'd', 'a', 'f', 'd', 'a', 'd', 'f', 'a', 's', 'f', 'd', 'a', 'f', 'a', 'f', 'd', 'a'] (1) ['1', '2', '3', '2', '4', '6', '4', '6', '5', '1', '6', '4', '8', '1', '5', '6', '4', '1', '2', '7', '1', '1', '3', '0', '0', '2', '5', '8'] (2) [] (3)
def finditer(pattern, string, flags=0): """Return an iterator over all non-overlapping matches in the string. For each match, the iterator returns a match object. Empty matches are included in the result.""" return _compile(pattern, flags).finditer(string) finditer(pattern,string)查找模式,Return an iterator over all non-overlapping matches in the string.For each match,the iterator a match object.
import re
string = "dd12a32d46465fad1648fa1564fda127fd11ad30fa02sfd58afafda"
m = re.finditer("[a-z]",string)
print(m)
n = re.finditer("AB",string)
print(n)
<callable_iterator object at 0x7fa126441898> (1) <callable_iterator object at 0x7fa124d6b710> (2)
def compile(pattern, flags=0): "Compile a regular expression pattern, returning a pattern object." return _compile(pattern, flags)
def purge(): "Clear the regular expression caches" _cache.clear() _cache_repl.clear()
def template(pattern, flags=0): "Compile a template pattern, returning a pattern object" return _compile(pattern, flags|T)
import re
string = "dd12a32d46465fad1648fa1564fda127fd11ad30fa02sfd58afafda"
p = re.compile("[a-z]+") #先使用compile(pattern)进行编译
m = p.match(string) #然后进行匹配
print(m.group())
m = p.match("^[0-9]",'14534Abc')
import re
string = "dd12a32d41648f27fd11a0sfdda"
#^匹配字符串的开头,现在我们使用search()来匹配以数字开始的
m = re.search("^[0-9]",string) #匹配字符串开头以数字开始 (1)
print(m)
n = re.search("^[a-z]+",string) #匹配字符串开头以字母开始,如果是从开头匹配,就与search()没有太多的区别了 (2)
print(n.group())
import re
string = "15111252598"
#^匹配字符串的开头,现在我们使用search()来匹配以数字开始的
m = re.match("^[0-9]{11}$",string)
print(m.group())
import re
string = "1511\n1252598"
#点(·)是匹配除了换行符以外所有的字符
m = re.match(".",string) #点(·)是匹配任意字符,没有指定个数就匹配单个 (1)
print(m.group())
n = re.match(".+",string) #.+是匹配多个任意字符,除了换行符 (2)
print(n.group())
import re
string = "1511\n125dadfadf2598"
#[]匹配包含括号中的字符
m = re.findall("[5fd]",string) #匹配字符串中的5,f,d
print(m)
import re
string = "1511\n125dadfadf2598"
#[^]匹配包含括号中的字符
m = re.findall("[^5fd]",string) #匹配字符串除5,f,d之外的字符
print(m)
import re
string = "1511\n125dadfadf2598"
#*是匹配0个或多个的表达式
m = re.findall("\d*",string) #匹配0个或多个数字
print(m)
import re
string = "1511\n125dadfadf2598"
#(+)是匹配1个或多个的表达式
m = re.findall("\d+",string) #匹配1个或多个数字
print(m)
import re
string = "1511\n125dadfadf2598"
#(?)是匹配0个或1个的表达式
m = re.findall("\d?",string) #匹配0个或1个的表达式
print(m)
import re
string = "1511\n125dadfadf2598"
#(?)是匹配0个或1个的表达式
m = re.findall("\w",string) #匹配0个或1个的表达式
print(m)
import re
string = "1511\n125dadfadf2598"
#\W用来匹配字符串中的非字母和数字
m = re.findall("\W",string) #\W用来匹配字符串中的非字母和数字
print(m)
import re
string = "1511\n125d\ta\rdf\fadf2598"
#\s是用来匹配字符串中的任意空白字符,等价于[\n\t\r\f]
m = re.findall("\s",string) #\s用来匹配字符串中任意空白字符
print(m)
import re
string = "1511\n125d\ta\rdf\fadf2598"
#\S是用来匹配任意非空字符
m = re.findall("\S",string) #\S用来匹配日任意非空字符
print(m)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有