How to extract certain strings start with a symbol

2019-07-24 14:23发布

问题:

How can i extract all words start from symbol '$' from a text file?

File a (ascii) -->

        @ExtendedAttr = nvp_add(@ExtendedAttr, "severity", $severity,  
 "description", $description, "eventID", $eventID,
             "eventURL", $eventURL, "alertLevel", $alertLevel, 
      "eventStart", $eventStart,
             "eventSourceCount", $eventSourceCount, "eventSourceTable", 
$eventSourceTable, "eventDestCount", $eventDestCount)

I want the output to be like this (all in new line) :

$severity
$description
$eventID
$eventURL
$alertLevel
$eventStart
$eventSourceCount
$eventSourceTable
$eventDestCount

回答1:

Use regex:

>>> import re
>>> with open('filename') as f:
...     ans = []
...     for line in f:
...         matches = re.findall(r'(?<!\w)(\$\w+)', line)
...         ans.extend(matches)
...         
>>> print ans
['$severity', '$description', '$eventID', '$eventURL', '$alertLevel', '$eventStart', '$eventSourceCount', '$eventSourceTable', '$eventDestCount']

Now use str.join to get the expected output:

>>> print "\n".join(ans)
$severity
$description
$eventID
$eventURL
$alertLevel
$eventStart
$eventSourceCount
$eventSourceTable
$eventDestCount


回答2:

Use regular expressions, noticing the escaping of $ (usually line-end) with \. Read the entire file at once with f.read() (Which can also be extracted to another line for enhanced readability)

import re

with open("filename", "r") as f:
...     matches = re.findall("(\$\w+)", f.read())
print matches