Python正则表达式 - 在背靠背中拆分字符串,中间有[可能]空格

时间:2013-07-18 15:24:42

标签: python regex line-breaks

我想完成标题所说的内容。所以我有下面显示的以下字符串,我希望能够找到所有双行换行符(可能在下面之间有空格,可能不是):

input = """4. A drawer locli:ing device for locl@.ing ,t  
     15 tier of draivers, one of which is lock controllecl, comprising

     twc, drawer retainina m--mbe , rs loica@ted at the front of th-. 
     drawer@' oiie acljacept each side of the tier of dra,-wers ar,d
      arranged to 
     overlap the front of the,"""

output = re.finditer('\n[\S+]\n', nameString)?????????????????????

output[0] = "4. A drawer locli:ing device for locl@.ing ,t  
     15 tier of draivers, one of which is lock controllecl, comprising"
output[1] = "twc, drawer retainina m--mbe , rs loica@ted at the front of th-. 
     drawer@' oiie acljacept each side of the tier of dra,-wers ar,d
      arranged to 
     overlap the front of the,"

1 个答案:

答案 0 :(得分:1)

看看这个:

>>> data = """4. A drawer locli:ing device for locl@.ing ,t  
         15 tier of draivers, one of which is lock controllecl, comprising

         twc, drawer retainina m--mbe , rs loica@ted at the front of th-. 
         drawer@' oiie acljacept each side of the tier of dra,-wers ar,d
          arranged to 
         overlap the front of the,"""

现在我们导入正则表达式:

>>> import re

然后我们分开它:

>>> r = re.split(r'\n\s*\n', data) # for more than 2 newlines: r'\n[\s\n]*\n'

现在显示结果:

>>> r[0]
'4. A drawer locli:ing device for locl@.ing ,t  \n         15 tier of draivers, one of which is lock controllecl, comprising'
>>> r[1]
"         twc, drawer retainina m--mbe , rs loica@ted at the front of th-. \n         drawer@' oiie acljacept each side of the tier of dra,-wers ar,d\n          arranged to \n         overlap the front of the,"