MultiLine RegEx用于以数字开头的文本块

时间:2011-06-27 17:47:28

标签: ruby regex

我的文字看起来像这样:

8    (10.0.0.1, 224.0.255.36) in e3/2 (e3/2), cnt=0
     Sparse Mode, RPT=0 SPT=1 Reg=0 L2Reg=0 RegSupp=0 LSrc=0 LRcv=0
     upstream neighbor=11.22.44.55
     num_oifs = 1
     L3 (HW) 1: tag e4/19(VL804)
     Flags (0x000280a1)
        sm=1 ssm=0 fast=1 slow=0 leaf=0 prun=0 frag=0 tag=1 needRte=0 msdp_adv=0
     age=0 fid: 8079 mvid:      3 
9    (*, 224.0.255.118) RP 11.22.33.44, in e3/2 (NIL), cnt=0
     Sparse Mode, RPT=1 SPT=0 Reg=0 L2Reg=0 RegSupp=0 LSrc=0 LRcv=0
     upstream neighbor=11.22.44.55
     num_oifs = 2
     L3 (SW) 1: tag e4/19(VL804)
     L3 (SW) 2: tag e4/20(VL805)
     Flags (0x002200a0)
        sm=1 ssm=0 fast=0 slow=0 leaf=0 prun=0 frag=0 tag=1 needRte=0 msdp_adv=0
     age=0 fid: none mvid: none 
10   (10.0.0.1, 224.0.255.118) in e3/2 (e3/2), cnt=0
     Sparse Mode, RPT=0 SPT=1 Reg=0 L2Reg=0 RegSupp=0 LSrc=0 LRcv=0
     upstream neighbor=11.22.44.55
     num_oifs = 0
     Flags (0x000280a1)
        sm=1 ssm=0 fast=1 slow=0 leaf=0 prun=0 frag=0 tag=1 needRte=0 msdp_adv=0
     age=0 fid: 80c9 mvid:    182 
11   (*, 224.2.254.32) RP 11.22.33.44, in e3/2 (NIL), cnt=0
     Sparse Mode, RPT=1 SPT=0 Reg=0 L2Reg=0 RegSupp=0 LSrc=0 LRcv=0
     upstream neighbor=11.22.44.55
     num_oifs = 1
     L3 (SW) 1: tag e4/19(VL804)
     Flags (0x002200a0)
        sm=1 ssm=0 fast=0 slow=0 leaf=0 prun=0 frag=0 tag=1 needRte=0 msdp_adv=0
     age=0 fid: none mvid: 
474  (*, 224.0.254.114) RP 11.22.33.44, in e3/2 (NIL), cnt=0
     Sparse Mode, RPT=1 SPT=0 Reg=0 L2Reg=0 RegSupp=0 LSrc=0 LRcv=0
     upstream neighbor=11.22.44.55
     num_oifs = 0
     Flags (0x002200a0)
        sm=1 ssm=0 fast=0 slow=0 leaf=0 prun=0 frag=0 tag=1 needRte=0 msdp_adv=0
     age=0 fid: none mvid: none     

我想为每个块返回一个匹配项。我正在使用Ruby。

我已经尝试过像这样的正则表达式,但它无法正常工作:

(^\d+[\S\s].*?)

1 个答案:

答案 0 :(得分:0)

目前尚不清楚您要做什么,但如果您只想将整个文本拆分为数组,那么:

your_string.split(/(?<=.)(?=^\d)/)

或更仔细,

your_string.split(/(?<=.)(?=^\d+\s+)/)

正则表达式的问题在于,默认情况下,.与多行不匹配,包括“\ n”。为此,您需要在正则表达式m之后添加/ /m