[英]python regex: how to split string into distinct groups based on alphabets, digits and punctuation
我正在使用python 2.7學習正則表達式
給出一個句子(假設小寫和ascii),例如:
input = 'i like: a, b, 007 and c!!'
我如何將輸入字符串標記為
['i', 'like', ':', 'a', ',', 'b', ',', '007', 'and', 'c', '!!']
我可以編寫自動機並用C ++編寫轉換矩陣代碼,但我想在python中執行此操作
我無法想出一個可以同時匹配這些不同類別的字母,數字和標點符號的正則表達式。
我在這里和這里看過幾個stackoverflow帖子,但是並沒有完全按照他們的方法。
我已經嘗試了一段時間,我很感激你的幫助。
PS:這不是一個家庭作業問題
>>> from string import punctuation
>>> text = 'i like: a, b, 007 and c!!'
>>> re.findall('\w+|[{0}]+'.format(punctuation),text)
['i', 'like', ':', 'a', ',', 'b', ',', '007', 'and', 'c', '!!']
這也有效,但如果找不到字母數字字符,則會找到任何非空白字符
>>> re.findall('\w+|\S+',text)
['i', 'like', ':', 'a', ',', 'b', ',', '007', 'and', 'c', '!!']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.