繁体   English   中英

正则表达式仅搜索单词

[英]Regex only searching for words

我有这个正则表达式,可以在http://regexpal.com/中正常工作:

[^-:1234567890/.,\s]*

我想在一个充满( , . # "" \\n \\s ... etc)的段落中找到

但是在我的代码中,我看不到我正在指定的结果:

def words(lines):
    words_pattern = re.compile(r'[^-:1234567890/.,\s]*')
    li = []
    for m in lines:
        e = words_pattern.search(m)
        if e:
            match = e.group()
            li.append(match)
    return li

li = [u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'']

有什么建议吗? 也许我没有以正确的方式从一个地方越过正则表达式

提前致谢

编辑

更确切地说,我确实想要:ñáéíó和ú

谢谢

如果只需要字母,则可以使用string.ascii_letters

>>> from string import ascii_letters
>>> import re
>>> s = 'this is 123 some text! that has someñ \n other stuff.'
>>> re.findall('[{}]+'.format(ascii_letters), s)
['this', 'is', 'some', 'text', 'that', 'has', 'some', 'other', 'stuff']

您还可以从[A-Za-z]获得相同的行为(与string.ascii_letters本质上是相同的)

>>> re.findall('[A-Za-z]+', s)
['this', 'is', 'some', 'text', 'that', 'has', 'some', 'other', 'stuff']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM