Manish Choudhary
Manish Choudhary

Reputation: 78

get data only start with space Regex - Python

I would like get all lines start with space

Sample of initial text:

!
line con 0
 session-timeout 5 
 exec-timeout 5 0
 password 7 1239211A43054F0202D1D
 transport output none
line 2
 no activation-character
 no exec
 transport preferred none
 transport output pad telnet rlogin lapb-ta mop udptn v120 ssh
 stopbits 1
line vty 0 4
 session-timeout 5 
 access-class 125 in
 exec-timeout 5 0
 length 0
 transport input ssh
 transport output none

Result must be:

line con 0
 session-timeout 5 
 exec-timeout 5 0
 password 7 1239211A43054FB202D1D
 transport output none

I tried something in Python

result = re.findall('line\scon(?s).*?(?=^\w)', str(abovetext))

Upvotes: 2

Views: 65

Answers (2)

Ryszard Czech
Ryszard Czech

Reputation: 18641

Use

(?m)^line\scon.*(?:\n .+)*

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  (?m)                     set flags for this block (with ^ and $
                           matching start and end of line) (case-
                           sensitive) (with . not matching \n)
                           (matching whitespace and # normally)
--------------------------------------------------------------------------------
  ^                        the beginning of a "line"
--------------------------------------------------------------------------------
  line                     'line'
--------------------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
  con                      'con'
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \n                       '\n' (newline)
--------------------------------------------------------------------------------
                             ' '
--------------------------------------------------------------------------------
    .+                       any character except \n (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )*                       end of grouping

Python code:

result = re.findall(r'^line\scon.*(?:\n .+)*', str(abovetext), re.M)

Upvotes: 2

unltd_J
unltd_J

Reputation: 494

strings are iterable and can be accessed by index

lines=[]
for line in text:
    if line[0] == ' ':
        lines.append(line)

This code will iterate over the text, and check if the first element in the line is equal to an empty string, which is the same as a space.

Upvotes: 0

Related Questions