Home > front end >  Logic and spaces in Regex
Logic and spaces in Regex

Time:03-09

I have the following Regex:

^(?P<port>[\w\-\>] )(?P<virtual_route>  None|None)\s (?P<port_state>\w)\s (?P<link_state>[\w] )\s (?P<auto_neg>[\w] )\s (?P<cfg_speed>[\d] ) (?P<actual_speed>[\d\s] )(?P<cfg_duplex>[\w] )

That I use on the following configuration:

MLAG-ISC>None       E       A   OFF 40000 40000 FULL FULL   NONE     1  Q CR4_1m
2        None       E      NP   OFF 10000       FULL                    NONE
3        None       E      NP   OFF 10000       FULL                    NONE
4        None       E      NP   OFF 10000       FULL                    NONE
MLAG-ISC>None       E       A   OFF 40000 40000 FULL FULL   NONE     1  Q CR4_1m
6        None       E      NP   OFF 10000       FULL                    NONE

Which gives me this result (enter image description here

This is the result I want however I would also like to capture the next FULL or empty, NONE or empty, 1 or empty and Q CR4_1m or NONE. I just can't seem to make it work because of the spaces in the rows 2,3,4 and 6.

Note that I am using Python3.

CodePudding user response:

If FULL, NONE and 1 are the only possible (though optional) values in that columns:

^(?P<port>[\w\-\>] )(?P<virtual_route>  None|None)\s (?P<port_state>\w)\s (?P<link_state>[\w] )\s (?P<auto_neg>[\w] )\s (?P<cfg_speed>[\d] ) (?P<actual_speed>\d*)\s (?P<cfg_duplex>[\w] )\s ((?:FULL)?)\s ((?:NONE)?)\s (1?)\s ([\w ] )

otherwise:

^(?P<port>[\w\-\>] )(?P<virtual_route>  None|None)\s (?P<port_state>\w)\s (?P<link_state>[\w] )\s (?P<auto_neg>[\w] )\s (?P<cfg_speed>[\d] ) (?P<actual_speed>\d*)\s (?P<cfg_duplex>[\w] )[^\S\r\n] (\w*)[^\S\r\n] (\w*)[^\S\r\n] (\d*)[^\S\r\n] ([\w ] )
  • Related