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 a string between 2 words from a text file OscarBoots 2 433 Nov-02-2021, 08:50 AM
Last Post: ibreeden
  Extract text based on postion and pattern guddu_12 2 497 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  Replace String in multiple text-files [SOLVED] AlphaInc 5 1,363 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  fuzzywuzzy search string in text file marfer 9 1,055 Aug-03-2021, 02:41 AM
Last Post: deanhystad
  Open and read multiple text files and match words kozaizsvemira 3 4,795 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  Reading Multiple text Files in pyhton Fatim 1 670 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Extract specific sentences from text file Bubly 3 1,003 May-31-2021, 06:55 PM
Last Post: Larz60+
  extract color text from PDF Maha 0 660 May-31-2021, 04:05 PM
Last Post: Maha
  How to extract specific key value pair from string? aditi06 0 1,042 Apr-15-2021, 06:26 PM
Last Post: aditi06
  Extract continuous numeric characters from a string in Python Robotguy 2 1,056 Jan-16-2021, 12:44 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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