正则表达式(regular)知识(整理)(2)

ddAfsAdAdfAdfAfdAfdAdfAsfdAfAfdA        --(1)
  ddAfsAdadfadfafdafdadfasfdafafda        -- (2)
  ddafsadadfadfafdafdadfasfdafafda        --(3)

上面代码(1)是没有指定匹配的个数,那么默认是把所有的都匹配了;(2)处指定了匹配的个数,那么只匹配指定个数的;(3)处要匹配的正则pattern不在字符串中,则返回原来的字符串。

重点:(1)可以指定匹配个数,不指定匹配所有;(2)如果匹配不到会返回原来的字符串;

    (5)subn(pattern,repl,string,count=0,flags=0)

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)

上面注释Return a 2-tuple containing(new_string,number):返回一个元组,用于存放正则匹配之后的新的字符串和匹配的个数(new_string,number)。

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)

运行结果如下:

('ddAfsAdAdfAdfAfdAfdAdfAsfdAfAfdA', 11)     (1)
  ('ddAfsAdAdfadfafdafdadfasfdafafda', 3)      (2)
  ('ddafsadadfadfafdafdadfasfdafafda', 0)       (3)

从上面代码输出的结果可以看出,sub()和subn(pattern,repl,string,count=0,flags=0)可以看出,两者匹配的效果是一样的,只是返回的结果不同而已,sub()返回的还是一个字符串,而subn()返回的是一个元组,用于存放正则之后新的字符串,和替换的个数。

    (6)split(pattern,string,maxsplit=0,flags=0)   

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)

从(1)处可以看出,如果字符串开头或者结尾包括要分割的字符串,后面元素会是一个"";(2)处我们可以指定要分割的次数;(3)处如果要分割的字符串不存在列表中,则把原字符串放在列表中。

(7)findall(pattern,string,flags=)

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)

运行结果如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjsgxs.html