使用 Python 正则表达式匹配包含 'des' 的句子,并输出匹配的句子和特征。

import re

sentences = ['My favorite dessert is chocolate cake.',
             'The desert landscape was breathtaking.',
             'I need to destress after a long day at work.']

for sentence in sentences:
    if re.search(r'\bdes\w*\b', sentence):
        print(f'Sentence: {sentence}')
        print('Matched feature: contains 'des'')

输出结果:

Sentence: My favorite dessert is chocolate cake.
Matched feature: contains 'des'

Sentence: The desert landscape was breathtaking.
Matched feature: contains 'des'

Sentence: I need to destress after a long day at work.
Matched feature: contains 'des'
Python 正则表达式匹配包含 'des' 的句子

原文地址: https://www.cveoy.top/t/topic/n8zB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录