Python俱乐部
Python
小课题
京东优惠券
用newstring替换subject中所有与正则表达式regex匹配的子串
result, number = re.subn(regex, newstring, subject)
reobj = re.compile(regex) result, number = reobj.subn(newstring, subject)
result = re.split(regex, subject)
reobj = re.compile(regex) result = reobj.split(subject)
下面列出Python正则表达式的几种匹配用法:
regex=ur"..." #正则表达式 if re.search(regex, subject): do_something() else: do_anotherthing()
regex=ur"...\Z" #正则表达式末尾以\Z结束 if re.match(regex, subject): do_something() else: do_anotherthing()
regex=ur"..." #正则表达式 match = re.search(regex, subject) if match: # match start: match.start() # match end (exclusive): match.end() # matched text: match.group() do_something() else: do_anotherthing()
(Get the part of a string matched by the regex)
regex=ur"..." #正则表达式 match = re.search(regex, subject) if match: result = match.group() else: result = ""
(Get the part of a string matched by a capturing group)
regex=ur"..." #正则表达式 match = re.search(regex, subject) if match: result = match.group(1) else: result = ""
(Get the part of a string matched by a named group)
regex=ur"..." #正则表达式 match = re.search(regex, subject) if match: result = match.group("groupname") else: result = ""
(Get an array of all regex matches in a string)
result = re.findall(regex, subject)
(Iterate over all matches in a string)
for match in re.finditer(r"<(.*?)\s*.*?/\1>", subject) # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()
(Create an object to use the same regex for many operations)
reobj = re.compile(regex)
(use regex object for if/else branch whether (part of) a string can be matched)
reobj = re.compile(regex) if reobj.search(subject): do_something() else: do_anotherthing()
(use regex object for if/else branch whether a string can be matched entirely)
reobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束 if reobj.match(subject): do_something() else: do_anotherthing()
(Create an object with details about how the regex object matches (part of) a string)
reobj = re.compile(regex) match = reobj.search(subject) if match: # match start: match.start() # match end (exclusive): match.end() # matched text: match.group() do_something() else: do_anotherthing()
(Use regex object to get the part of a string matched by the regex)
reobj = re.compile(regex) match = reobj.search(subject) if match: result = match.group() else: result = ""
(Use regex object to get the part of a string matched by a capturing group)
reobj = re.compile(regex) match = reobj.search(subject) if match: result = match.group(1) else: result = ""
(Use regex object to get the part of a string matched by a named group)
reobj = re.compile(regex) match = reobj.search(subject) if match: result = match.group("groupname") else: result = ""
(Use regex object to get an array of all regex matches in a string)
reobj = re.compile(regex) result = reobj.findall(subject)
(Use regex object to iterate over all matches in a string)
reobj = re.compile(regex) for match in reobj.finditer(subject): # match start: match.start() # match end (exclusive): match.end() # matched text: match.group()