RegEx splits text string into dict as a group

I am trying to output RegEx string and add to the dictionary as a group of text, and for this I use re.split().

My string output:

mpathag (36005076801b2014804000000000001cd) dm-7 Test ,2145
size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw
|-+- policy='service-time 0' prio=50 status=active
| |- 12:0:1:3 sdas  66:192 active ready  running
| `- 13:0:1:3 sdbi  67:192 active ready  running
`-+- policy='service-time 0' prio=10 status=enabled
  |- 12:0:0:3 sdak  66:64  active ready  running
  `- 13:0:0:3 sdba  67:64  active ready  running
mpathz (36005076801b2014804000000000001c4) dm-0 Test ,2145
size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw
|-+- policy='service-time 0' prio=50 status=active
| |- 12:0:0:0 sdah  66:16  active ready  running
| `- 13:0:0:0 sdax  67:16  active ready  running
 `-+- policy='service-time 0' prio=10 status=enabled
   |- 10:0:0:0 sdb   8:16   active ready  running
   `- 13:0:1:0 sdbf  67:144 active ready  running

Here is my parsing code:

    output = "mpathag (36005076801b2014804000000000001cd) dm-7 Test ,2145\
    size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw\
    |-+- policy='service-time 0' prio=50 status=active\
    | |- 12:0:1:3 sdas  66:192 active ready  running\
    | `- 13:0:1:3 sdbi  67:192 active ready  running\
    `-+- policy='service-time 0' prio=10 status=enabled\
      |- 12:0:0:3 sdak  66:64  active ready  running\
      `- 13:0:0:3 sdba  67:64  active ready  running\
    mpathz (36005076801b2014804000000000001c4) dm-0 Test ,2145\
    size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw\
    |-+- policy='service-time 0' prio=50 status=active\
    | |- 12:0:0:0 sdah  66:16  active ready  running\
    | `- 13:0:0:0 sdax  67:16  active ready  running\
     `-+- policy='service-time 0' prio=10 status=enabled\
       |- 10:0:0:0 sdb   8:16   active ready  running\
       `- 13:0:1:0 sdbf  67:144 active ready  running"

    devices = re.split('mpath', output)
    for dev in devices:
       print dev

My problem is that re.split removes the template from text that can be broken without removing the delimiter?

The output results are as follows:

ag (36005076801b2014804000000000001cd) dm-7 Test ,2145    
size=488G features='1 queue_if_no_path' hwhandler='0' wp=rw    
|-+- policy='service-time 0' prio=50 status=active    
| |- 12:0:1:3 sdas  66:192 active ready  running    
| `- 13:0:1:3 sdbi  67:192 active ready  running    
`-+- policy='service-time 0' prio=10 status=enabled      
  |- 12:0:0:3 sdak  66:64  active ready  running      
  `- 13:0:0:3 sdba  67:64  active ready  running
+4
source share
1 answer

If the template includes a capture group, this group will not be excluded:

>>> re.split(',', '1,2,3')  # without capturing group
['1', '2', '3']
>>> re.split('(,)', '1,2,3')  # with capturing group
['1', ',', '2', ',', '3']
>>> xs = re.split('(,)', '1,2,3')
>>> [part1+part2 for part1, part2 in zip(xs[1::2], xs[2::2])]
[',2', ',3']

devices = re.split('(mpath)', output)
for part1, part2 in zip(devices[1::2], devices[2::2]):
    print part1 + part2
0
source

Source: https://habr.com/ru/post/1616339/


All Articles