簡體   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