_StringKeyLine(FileRead('test.txt'), '关键字', 'new.txt')
Func _StringKeyLine($str, $Key, $NewFile = '', $FOMode = 2)
;[可选]$NewFile 写入新文件的文件名
;[可选]$FOMode 写入新文件模式:= 1 追加;= 2 覆写
Local $sR, $Out, $FO
$sR = StringRegExp($str, '(?<=^|\n)\V*\Q' & $Key & '\E\V*', 3)
If @error Then Return SetError(1, 0, '')
For $i = 0 To UBound($sR) - 1
$Out &= $sR[$i] & @CRLF
Next
If $NewFile = '' Then Return $Out
If $FOMode <> 2 Then $FOMode = 1
$FO = FileOpen($NewFile, $FOMode)
FileWrite($FO, $Out)
FileClose($FO)
EndFunc ;==>_StringKeyLine
|