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 table from multiple pages sshree43 7 303 Aug-02-2022, 09:45 AM
Last Post: sshree43
Photo put a green boarder and text around multiple pictures jackosullivan 5 233 Jul-05-2022, 10:39 AM
Last Post: snippsat
  Extract text rektcol 6 374 Jun-28-2022, 08:57 AM
Last Post: Gribouillis
  Matching multiple parts in string fozz 31 2,100 Jun-13-2022, 09:38 AM
Last Post: fozz
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 527 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Editing text between two string from different lines Paqqno 1 496 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  Search multiple CSV files for a string or strings cubangt 7 1,896 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Extract a string between 2 words from a text file OscarBoots 2 915 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Extract text based on postion and pattern guddu_12 2 884 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  Replace String in multiple text-files [SOLVED] AlphaInc 5 2,764 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

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