Posts: 3 Threads: 1 Joined: Jan 2020 Jan-19-2020, 12:17 PM (This post was last modified: Jan-19-2020, 12:17 PM by batchen.) Hey, im using python3 and i use ansible. i get this ansible output : "['', 'PLAY [localhost] **', '', 'TASK [Gathering Facts] **', 'ok: [localhost]', '', 'TASK [Create a VM folder on given datacenter] **', 'ok: [localhost]', '', 'TASK [debug] ***', 'ok: [localhost] => {', ' 'msg': [', ' 'temp11579079896506',', ' 'temp1157902013446',', ' temp11579018229876',', ' 'temp2323',', ' 'temp11578823964592'', ' ]', '}', '', 'PLAY RECAP ****', 'localst: ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 ', '', '']"which is a string. i need python to extract the list after "'msg':" and for it to look like that : ['temp11579079896506', 'temp1157902013446','temp11579018229876', 'temp2323','temp11578823964592'] I just cant understand regex i really tried on my own, thanks Posts: 7,398 Threads: 123 Joined: Sep 2016 Jan-19-2020, 03:22 PM (This post was last modified: Jan-19-2020, 03:22 PM by snippsat.) Can try this. >>> import re >>> >>> r = re.search(r"msg\':\s\[(.*?)\]", lst) >>> r = r.group(1) >>> my_lst = re.findall(r"\w+", r) >>> my_lst ['temp11579079896506', 'temp1157902013446', 'temp11579018229876', 'temp2323', 'temp11578823964592'] >>> my_lst[-1] 'temp11578823964592' Posts: 3 Threads: 1 Joined: Jan 2020 (Jan-19-2020, 03:22 PM)snippsat Wrote: Can try this. >>> import re >>> >>> r = re.search(r"msg\':\s\[(.*?)\]", lst) >>> r = r.group(1) >>> my_lst = re.findall(r"\w+", r) >>> my_lst ['temp11579079896506', 'temp1157902013446', 'temp11579018229876', 'temp2323', 'temp11578823964592'] >>> my_lst[-1] 'temp11578823964592' yes it works but then i noticed that i posted the wrong string this is the real one : ['', 'PLAY [localhost] ************************************************************************************************************************************************************', '', 'TASK [Gathering Facts] ******************************************************************************************************************************************************', 'ok: [localhost]', '', 'TASK [Create a VM folder on given datacenter] *******************************************************************************************************************************', 'ok: [localhost]', '', 'TASK [debug] ****************************************************************************************************************************************************************', 'ok: [localhost] => {', ' "msg": [', ' "temp11579079896506",', ' "temp1157902013446",', ' "temp11579018229876",', ' "temp2323",', ' "temp11578823964592"', ' ]', '}', '', 'PLAY RECAP ******************************************************************************************************************************************************************', 'localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 ', '', '']no duble quote in the begining and "msg" , i changed output for my python3 so i can check, i know it doesn't start with "" at the beginning but this is a string type output. i tried to make changes in your code to re.search(r'"msg\":\s\[(.*?)\]', lst) it doesnt catch it. im sorry i hope you can help me again Posts: 7,398 Threads: 123 Joined: Sep 2016 Then it will be like this. >>> import re >>> >>> r = re.search(r"msg.*\[(.*?)\]", str(lst)) >>> r = r.group(1) >>> my_lst = re.findall(r"\w+", r) >>> my_lst ['temp11579079896506', 'temp1157902013446', 'temp11579018229876', 'temp2323', 'temp11578823964592'] Posts: 3 Threads: 1 Joined: Jan 2020 (Jan-19-2020, 04:59 PM)snippsat Wrote: Then it will be like this. >>> import re >>> >>> r = re.search(r"msg.*\[(.*?)\]", str(lst)) >>> r = r.group(1) >>> my_lst = re.findall(r"\w+", r) >>> my_lst ['temp11579079896506', 'temp1157902013446', 'temp11579018229876', 'temp2323', 'temp11578823964592'] Thank you so much!!!! |