专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Perl/Python

怎么用正规表达式查找不以某些字符结尾的单词

发布时间:2011-06-29 20:05:17 文章来源:www.iduyao.cn 采编人员:星星草
如何用正规表达式查找不以某些字符结尾的单词
例如现在有一篇英文文章,如何找出所有不以ly结尾的单词呢?

------解决方案--------------------
我怀疑是找以ly结尾的单词,因为这样比较少一点。不管了,我两种都写一下吧:
Python code
import re

txt = '''
Dementia is a loss of intellectual ability that affects memory, learning,
attention, thinking and language skills. People with dementia may forget
family members or not know what day it is. Sometimes they become angry or sad,
hear voices, or see things that are not there.

The team found that dementia was the largest cause of disability in the elderly
in areas other than rural India and Venezuela. Other major causes were stroke,
loss of use of arms or legs, arthritis, depression, eyesight problems and
gastrointestinal problems.

The study says the elderly are nine percent of the total population of low and
middle income countries today. But their numbers are growing quickly. They are
expected to reach twenty percent of the total population by the middle of the
century.

It found that in almost half the countries, children who were not breastfeed
were much more likely to have a disability than those who were. The same was
true of those who did not receive vitamin A supplements and those who were
underweight.

Children who did not take part in early learning activities or attend school
were also more likely to be disabled than those who did.
'''

regEndWith_ly = re.compile(r'[a-zA-Z]+?ly', re.I | re.S)
print regEndWith_ly.findall(txt)
regNotEndWith_ly = re.compile(r'\b[a-zA-Z]+?(?<!ly)\b', re.I | re.S)
print regNotEndWith_ly.findall(txt)
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: