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)
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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to extract specific key value pair from string? aditi06 0 90 Yesterday, 06:26 PM
Last Post: aditi06
  Extract continuous numeric characters from a string in Python Robotguy 2 286 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to print string multiple times on new line ace19887 7 647 Sep-30-2020, 02:53 PM
Last Post: buran
  convert string into multiple columns in python3 VAN 2 580 Sep-26-2020, 11:14 PM
Last Post: scidam
  extract a dictionary from a string berc 4 687 Jul-30-2020, 06:58 AM
Last Post: berc
  How to extract a single word from a text file buttercup 7 752 Jul-22-2020, 04:45 AM
Last Post: bowlofred
  Extract data from large string pzig98 1 520 Jul-20-2020, 12:39 AM
Last Post: Larz60+
  xml.etree.ElementTree extract string values matthias100 2 485 Jul-12-2020, 06:02 PM
Last Post: snippsat
  can't save text string branko316 2 594 May-31-2020, 02:46 PM
Last Post: branko316
  How to print string multiple times separated by delimiter Mekala 1 520 May-23-2020, 09:21 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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