小编Ale*_*rad的帖子

字符串掩码和正则表达式的偏移量

我有一个字符串,我尝试创建一个正则表达式掩码N,给出一个偏移量,显示单词的数量.假设我有以下字符串:

"The quick, brown fox jumps over the lazy dog."

我想在当时显示3个单词:

offset 0:"The quick, brown"
offset 1:"quick, brown fox"
offset 2:"brown fox jumps"
offset 3:"fox jumps over"
offset 4:"jumps over the"
offset 5:"over the lazy"
offset 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

3
推荐指数
1
解决办法
2951
查看次数

标签 统计

python ×1

regex ×1

regex-negation ×1