regex match in a string - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: regex match in a string (/thread-23828.html) |
regex match in a string - batchen - Jan-19-2020 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 RE: regex match in a string - snippsat - Jan-19-2020 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' RE: regex match in a string - batchen - Jan-19-2020 (Jan-19-2020, 03:22 PM)snippsat Wrote: Can try this. 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 RE: regex match in a string - snippsat - Jan-19-2020 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'] RE: regex match in a string - batchen - Jan-20-2020 (Jan-19-2020, 04:59 PM)snippsat Wrote: Then it will be like this. Thank you so much!!!! |