Python Forum

Full Version: How to extract multiple text from a string?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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)
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']