【发布时间】:2010-07-18 11:30:12
【问题描述】:
我有一个字符串,我尝试在其上创建一个正则表达式掩码,该掩码将显示N 的字数,给定一个偏移量。假设我有以下字符串:
"The quick, brown fox jumps over the lazy dog."
我想当时显示3个字:
偏移量0:"The quick, brown"
偏移量1:"quick, brown fox"
偏移量2:"brown fox jumps"
偏移量3: "fox jumps over"
偏移量4:"jumps over the"
偏移量5:"over the lazy"
偏移量6:"the lazy dog."
我正在使用 Python,并且一直在使用以下简单的正则表达式来检测 3 个单词:
>>> import re>>> s = "The quick, brown fox jumps over the lazy dog.">>> re.search(r'(\w+\W*){3}', s).group()'The quick, brown '
但我不知道如何使用一种掩码来显示接下来的 3 个单词而不是开头的单词。我需要保留标点符号。
【问题讨论】:
标签: python regex regex-negation