0

I have the following string, in this example there are 4 rows, but there could be more:

  port  device  name           profile  settings
  ====  ======  =============  =======  =================
  1     ttyS1   name1          cas      9600 8N1 ssh none
  2     ttyS2   name2          cas      9600 8N1 ssh none
  3     ttyS3   name3          cas      9600 8N1 ssh none
  4     ttyS4   name4          cas      9600 8N1 ssh none

Press any key to continue...

And I need to get data only from the port and name columns, and assign it to an array of arrays, or several arrays.

e.g.

['1', 'name1']
['2', 'name2']
['3', 'name3']
['4', 'name4']

or

[['1', 'name1'],['2', 'name2'],['3', 'name3'],['4', 'name4']]

Could anyone give me any ideas on how could this be accomplished?

1
  • 1
    Is there a specific issue? Please see How to Ask, help center. Commented May 5, 2021 at 17:56

3 Answers 3

3

With your shown samples, please try following. Written and tested in Python3.8, using findall function of Python.

import re
var="""  port  device  name           profile  settings
====  ======  =============  =======  =================
1     ttyS1   name1          cas      9600 8N1 ssh none
2     ttyS2   name2          cas      9600 8N1 ssh none
3     ttyS3   name3          cas      9600 8N1 ssh none
4     ttyS4   name4          cas      9600 8N1 ssh none"""

re.findall(r'^\s+?(\d+).*?\S+\s+(\S+).*$',var,re.M)

Output will be as follows:

[('1', 'name1'), ('2', 'name2'), ('3', 'name3'), ('4', 'name4')]

Explanation: Adding detailed explanation for above regex.

^\s+?      ##Checking from starting of value spaces occurrences 1 or more optional.
(\d+)      ##Creating 1st capturing group which has continuous digits in it, port number.
.*?\S+\s+  ##using non greedy match to match till non space value(1 or more) followed by 1 or more spaces.
(\S+)      ##Creating 2nd capturing group which has all non space values(names) in it.
.*$        ##Matching rest of the values here.
Sign up to request clarification or add additional context in comments.

Comments

2
import re

txt = """
  port  device  name           profile  settings
  ====  ======  =============  =======  =================
  1     ttyS1   name1          cas      9600 8N1 ssh none
  2     ttyS2   name2          cas      9600 8N1 ssh none
  3     ttyS3   name3          cas      9600 8N1 ssh none
  4     ttyS4   name4          cas      9600 8N1 ssh none

Press any key to continue...
"""

r = re.compile(r"^[^\d]+(\d+)\s*[^\s]+\s*([^\s]+)", flags=re.M)

out = r.findall(txt)
print(out)

Prints:

[('1', 'name1'), ('2', 'name2'), ('3', 'name3'), ('4', 'name4')]

Comments

2

Another users already responted with best solutions but this is my simple solution :D

stringOrig = """
  port  device  name           profile  settings
  ====  ======  =============  =======  =================
  1     ttyS1   name1          cas      9600 8N1 ssh none
  2     ttyS2   name2          cas      9600 8N1 ssh none
  3     ttyS3   name3          cas      9600 8N1 ssh none
  4     ttyS4   name4          cas      9600 8N1 ssh none

Press any key to continue...
"""

stringLines = stringOrig.split("\n")
linesContent = stringLines[3:-3]
lineValues = list(
  map(lambda l: 
      list(
        filter(lambda x : x != "", l.split(" "))
      )[0:3:2]
      , linesContent
  )
)
print(lineValues)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.