作者a9 (a9)
看板Python
标题Re: [问题] re 用法
时间Sat Jun 28 00:11:32 2008
※ 引述《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