我有一个字符串,我尝试在其上创建一个正则表达式掩码,该掩码将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 个单词而不是开头的单词。我需要保留标点符号。