※ 引述《plom (plom)》之銘言:
: 請教各位我想找出字串中的英文字,用 re.compile(r'[\w]+')
: 但是像 abc-def,abc -def, abc-def, 不想被拆開為 abc 和 def 兩個單字, 仍要為 abc-def
: 請問該如何做呢?
[\w]+\s?-\s?[\w]+|[\w]+
我猜原PO的意思是想要將中間有-的視為同一個單字
測試字串如下
abc abc-def abc - def abc- def abc -def
因此先 match 具有-連接符號的單字 xxx-xxx
再 match 沒有連接符號的單字
抓出
abc
abc-def
abc - def
abc- def
abc -def
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 221.169.187.111