正则表达式代码实现部分
创建特定的搜索模式
string = "The quick brown fox jumps over the lazy dog."
string_list = string.split()
pattern = re.compile(r"The", re.I)
count = 0
for word in string_list:
if pattern.search(word):
count += 1
print("Output #38: {0:d}".format(count))
核心代码在于第三行,规定了搜索模式
re.compile 函数
将文本形式的模式编译成为编译后的正则表达式,我认为就是规定了一个如何搜索文本的规则
r”The“
r表示Python不处理转义字符,但是本次测试代码中没有出现
re.I
表示识别是不区分大小写的
另外一个示例
# 在字符串中每次找到模式时将其打印出来
string = "The quick brown fox jumps over the lazy dog."
string_list = string.split()
pattern = re.compile(r"(?P<match_word>The)", re.I)</match_word>
print("Output #39:")
for word in string_list:
if pattern.search(word):
print("{:s}".format(pattern.search(word).group('match_word')
第一个新代码片段是 (?P<name>),这是一个出现在 re.compile 函数中的元字符。这个元字符使匹配的字符串可以在后面的程序中通过组名符号 <name> 来引用。在这个示例中,这个组被称为 <match_word>。
元字符:指那些在正则表达式中具有特殊意义的专用字符,可以用来规定其前导字符(即位于元字符前面的字符)在目标对象中的出现模式
最后一个示例
# 使用字母“a”替换字符串中的单词“the”
string = "The quick brown fox jumps over the lazy dog."
string_to_find = r"The"
pattern = re.compile(string_to_find, re.I)
print("Output #40: {:s}".format(pattern.sub("a", string)
使用 re.sub 函数来在文本中用一种模式替换另一种模式