Capture Substring from list elements

Question:

Having list of strings as below:

    ['Communcation between: ACC_Verkehrssinn and SG_ACC_22nUsing testing method: MinMaxMid', 
     'Communcation between: ACC_Status_Laengs and SG_ACC_22nUsing testing method: MinMaxMid',
     'Signal Name:WLA_FC1_Obj10angleLeftnCommuncation between: SG_WLA_FC1_Obj01_30 and 
      SG_WLA_FC1_Obj01_30nUsing testing method: MinMaxMid', 
     'Signal Name:WLA_FC1_Obj10angleRightnCommuncation between: SG_WLA_FC1_Obj01_30 and 
      SG_WLA_FC1_Obj01_30nUsing testing method: MinMaxMid']

Want to read list elements which contains ‘Signal Name:’ and capture its value. Here in this case output should be:

WLA_FC1_Obj10angleLeft
WLA_FC1_Obj10angleRight

Any help is appreciated.

Asked By: shekharsabale

||

Answers:

We can use a list comprehension here:

inp = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22nUsing testing method: MinMaxMid', 'Communcation between: ACC_Status_Laengs and SG_ACC_22nUsing testing method: MinMaxMid', 'Signal Name:WLA_FC1_Obj10angleLeftnCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30nUsing testing method: MinMaxMid', 'Signal Name:WLA_FC1_Obj10angleRightnCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30nUsing testing method: MinMaxMid']
output = [re.search(r'Signal Name:s*(S+)', x).group(1) for x in inp if 'Signal Name:' in x]
print(output)

This prints:

['WLA_FC1_Obj10angleLeft', 'WLA_FC1_Obj10angleRight']
Answered By: Tim Biegeleisen

Try this approach without regex:

mylist = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22nUsing testing method: MinMaxMid', 
     'Communcation between: ACC_Status_Laengs and SG_ACC_22nUsing testing method: MinMaxMid',
     'Signal Name:WLA_FC1_Obj10angleLeftnCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30nUsing testing method: MinMaxMid', 
     'Signal Name:WLA_FC1_Obj10angleRightnCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30nUsing testing method: MinMaxMid']

values = []
for mystr in mylist:
    if mystr.find("Signal Name:") != -1:
        mystr = mystr[12:]
        index = mystr.find("n")
        mystr = mystr[:index]
        values.append(mystr)

print(values)

Output:

['WLA_FC1_Obj10angleLeft', 'WLA_FC1_Obj10angleRight']

The output gets saved in values list. You may access the individual elements to get what you want.

Answered By: NotShrirang

We can combine all strings into one large string, and use re.findall() to search for all words between the word "Signal Name:" and "n"

Hope this helps.

import re
strings = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22nUsing testing method: MinMaxMid', 
     'Communcation between: ACC_Status_Laengs and SG_ACC_22nUsing testing method: MinMaxMid',
     'Signal Name:WLA_FC1_Obj10angleLeftnCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30nUsing testing method: MinMaxMid', 
     'Signal Name:WLA_FC1_Obj10angleRightnCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30nUsing testing method: MinMaxMid']
all_strings = ''.join(strings)
print(re.findall("Signal Name:(.*)n", all_strings))

Output

['WLA_FC1_Obj10angleLeft', 'WLA_FC1_Obj10angleRight']
Answered By: DrCorgi
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.