import re
match = {} # create a dictionary of words-to-replace and words-to-replace-with
f = open("filename", "r")
data = f.read() # string of all file content
def replace_all(text, dic):
for i, j in dic.items():
text = re.sub(r"\b%s\b" % i, j, text)
# r"\b%s\b"% enables replacing by whole word matches only
return text
data = replace_all(data, match)
print(data) # you can copy and paste the result to whatever file you like
Вы можете сделать это с помощью Select-String в PowerShell 2.0 следующим образом:
Select-String F\d\d -input $string -AllMatches | Foreach {$_.matches}
Некоторое время назад я запросил оператор -matchall
в MS Connect, и это предложение было закрыто как исправленное с помощью следующего комментария:
«Это исправлено параметром -allmatches для строки выбора».
$String = @'
MemberProgram PackageID="12345678" ProgramName="Install"/
MemberProgram PackageID="87654321" ProgramName="Install"/
MemberProgram PackageID="21436587" ProgramName="Install"/
MemberProgram PackageID="78563412" ProgramName="Install"/
'@
([regex]'(?<=PackageID=\")\d+(?=\")').Matches($String).value