Python Forum
How to extract multiple text from a string?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to extract multiple text from a string?
#1
Question 
Hi All,

Would anyone know how to create a list of a substring prior to "==>" ?

Example String input:
'\r\nExecuting: /usr/bin/dig +short -4 @120.17.72.174 a hello.com\r\n\r\nSun Feb 28 06:49:59\r\n\t20.1.1.1 => 84(%28)\t\r\n\t20.1.1.2 => 111(%37)\t\r\n\tCNAME.RETURNED.COM => 105(%35)\t\r\n\tQueries=300 Duration=3 secs. QPS=100\r\n'

Desired List to create from above:
["20.1.1.1","20.1.1.2","CNAME.RETURNED.COM"]

Note:
There can be more than anywhere from 0 to infinity items in the above list

Thank you!
CG
Reply
#2
something like
spam = '\r\nExecuting: /usr/bin/dig +short -4 @120.17.72.174 a hello.com\r\n\r\nSun Feb 28 06:49:59\r\n\t20.1.1.1 => 84(%28)\t\r\n\t20.1.1.2 => 111(%37)\t\r\n\tCNAME.RETURNED.COM => 105(%35)\t\r\n\tQueries=300 Duration=3 secs. QPS=100\r\n'
eggs = [item.split('=>')[0].strip() for item in spam.splitlines() if '=>' in item]
print(eggs)
chatguy likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
How far back from the item are you looking? If you just want everything back to a space then you could look for a (capturing) set of at least one consecutive non-whitespace characters (\S+), followed by some optional whitespace \s*, and then the target string =>.

>>> import re
>>> re.findall("(\S+)\s*=>", '\r\nExecuting: /usr/bin/dig +short -4 @120.17.72.174 a hello.com\r\n\r\nSun Feb 28 06:49:59\r\n\t20.1.1.1 => 84(%28)\t\r\n\t20.1.1.2 => 111(%37)\t\r\n\tCNAME.RETURNED.COM => 105(%35)\t\r\n\tQueries=300 Duration=3 secs. QPS=100\r\n')
['20.1.1.1', '20.1.1.2', 'CNAME.RETURNED.COM']
chatguy likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 491 Nov-08-2023, 12:18 AM
Last Post: evilcode1
Sad How to split a String from Text Input into 40 char chunks? lastyle 7 1,054 Aug-01-2023, 09:36 AM
Last Post: Pedroski55
  splitting file into multiple files by searching for string AlphaInc 2 812 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  extract table from multiple pages sshree43 8 5,079 Dec-12-2022, 10:34 AM
Last Post: arvin
  extract only text strip byte array Pir8Radio 7 2,787 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Extract only certain text which are needed Calli 26 5,600 Oct-10-2022, 03:58 PM
Last Post: deanhystad
Photo put a green boarder and text around multiple pictures jackosullivan 5 1,402 Jul-05-2022, 10:39 AM
Last Post: snippsat
  Extract text rektcol 6 1,620 Jun-28-2022, 08:57 AM
Last Post: Gribouillis
  Matching multiple parts in string fozz 31 6,056 Jun-13-2022, 09:38 AM
Last Post: fozz
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 2,018 Apr-27-2022, 12:44 PM
Last Post: hasiro

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020