Home > database >  Capture Substring from list elements
Capture Substring from list elements

Time:07-31

Having list of strings as below:

    ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 
     'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid',
     'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and 
      SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 
     'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and 
      SG_WLA_FC1_Obj01_30\nUsing 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.

CodePudding user response:

We can use a list comprehension here:

inp = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid', 'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing 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']

CodePudding user response:

Try this approach without regex:

mylist = ['Communcation between: ACC_Verkehrssinn and SG_ACC_22\nUsing testing method: MinMaxMid', 
     'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid',
     'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 
     'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing 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.

CodePudding user response:

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_22\nUsing testing method: MinMaxMid', 
     'Communcation between: ACC_Status_Laengs and SG_ACC_22\nUsing testing method: MinMaxMid',
     'Signal Name:WLA_FC1_Obj10angleLeft\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid', 
     'Signal Name:WLA_FC1_Obj10angleRight\nCommuncation between: SG_WLA_FC1_Obj01_30 and SG_WLA_FC1_Obj01_30\nUsing testing method: MinMaxMid']
all_strings = ''.join(strings)
print(re.findall("Signal Name:(.*)\n", all_strings))

Output

['WLA_FC1_Obj10angleLeft', 'WLA_FC1_Obj10angleRight']
  • Related