【python】一文学会使用正则表达式

360影视 2025-02-09 10:18 3

摘要:import retext = "Search this string for patterns."match = re.search(r"patterns", text)if match: print("pattern found!")

要在字符串中找到一个与模式匹配的匹配项:

import retext = "Search this string for patterns."match = re.search(r"patterns", text)if match: print("pattern found!")

编译一个用于重复使用的正则表达式:

pattern = re.compile(r"patterns")match = pattern.search(text)

检查字符串是否以模式开头或结尾:

if re.match(r"^Search", text): print("Starts with 'Search'")if re.search(r"patterns.$", text): print("Ends with 'patterns.'")

要查找字符串中所有模式出现的位置:

all_matches = re.findall(r"t\w+", text) # Finds words starting with 't'print(all_matches)

要在字符串中替换模式出现:

replaced_text = re.sub(r"string", "sentence", text)print(replaced_text)

将字符串按模式出现分割:

words = re.split(r"\s+", text) # Split on one or more spacesprint(words)

匹配特殊字符时,请将其转义:

escaped = re.search(r"\bfor\b", text) # \b is a word boundary

将模式的部分分组并提取它们的值:

match = re.search(r"(\w+) (\w+)", text)if match: print(match.group) # The whole match print(match.group(1)) # The first group

定义不捕获的组:

match = re.search(r"(?:\w+) (\w+)", text)if match: print(match.group(1)) # The first (and only) group

匹配基于其前后内容而不包括其本身的模式:

lookahead = re.search(r"\b\w+(?= string)", text) # Word before ' string'lookbehind = re.search(r"(?

要使用类似 re.IGNORECASE 的标志来更改模式匹配方式:

case_insensitive = re.findall(r"search", text, re.IGNORECASE)print(case_insensitive)

将名称分配给组并通过名称引用它们:

match = re.search(r"(?P\w+) (?P\w+)", text)if match: print(match.group('first')) print(match.group('second'))

要使用re.MULTILINE标志匹配多行模式:

multi_line_text = "Start\nmiddle end"matches = re.findall(r"^m\w+", multi_line_text, re.MULTILINE)print(matches)

尽可能少地匹配字符,使用懒惰量词(*?、+?、??):

html = "

Title

"match = re.search(r"", html)if match: print(match.group) # Matches ''pattern = re.compile(r""" \b # Word boundary \w+ # One or more word characters \s # Space """, re.VERBOSE)match = pattern.search(text)

来源:自由坦荡的湖泊AI

相关推荐