Python Forum
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
regex match in a string
#1
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
Reply
#2
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'
Reply
#3
(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
Reply
#4
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']
Reply
#5
(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!!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 2,844 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Facing issue in python regex newline match Shr 6 9,929 Oct-25-2023, 09:42 AM
Last Post: Shr
  Failing regex, space before and after the "match" tester_V 6 4,208 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Regex pattern match WJSwan 2 4,089 Feb-07-2023, 04:52 AM
Last Post: WJSwan
  Match substring using regex Pavel_47 6 3,446 Jul-18-2022, 07:46 AM
Last Post: Pavel_47
  Match key-value json,Regex saam 5 9,141 Dec-07-2021, 03:06 PM
Last Post: saam
  if statement string match javiopro 2 2,864 Sep-04-2021, 05:56 PM
Last Post: javiopro
  Regex: a string does not starts and ends with the same character Melcu54 5 4,484 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  Help getting a string out of regex matt_the_hall 4 3,787 Dec-02-2020, 01:49 AM
Last Post: matt_the_hall
  regex.findall that won't match anything xiaobai97 1 3,192 Sep-24-2020, 02:02 PM
Last Post: DeaD_EyE

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.