可以试试~
_StringKeyLine2File(FileRead('test.txt'), '关键字1' & @LF & '关键字2', 'new.txt')
Func _StringKeyLine2File($sString, $sKey, $FileName = '', $FOMode = 2)
;$sKey - 关键字,如有多个关键字可用回车换行符连接,如 ‘关键字1’ & @LF & ‘关键字2’
;[可选]$FileName 写入文件的文件名
;[可选]$FOMode 打开写入文件模式:= 1 追加;= 2 覆写
Local $aSR, $sOut, $hFO
Local $aTmp, $sTmp = ''
$aTmp = StringRegExp($sKey, '\V+', 3)
If @error Then Return SetError(1, 0, '')
If UBound($aTmp) > 1 Then
For $ii = 0 To UBound($aTmp) - 1
$sTmp &= StringRegExpReplace($aTmp[$ii], '(\.|\||\*|\?|\+|\(|\)|\{|\}|\[|\]|\^|\$|\\)', '\\$1') & '|'
Next
$aSR = StringRegExp($sString, '(?m)^\V*(?:' & StringTrimRight($sTmp, 1) & ')\V*', 3)
Else
$aSR = StringRegExp($sString, '(?m)^\V*\Q' & $aTmp[0] & '\E\V*', 3)
EndIf
If @error Then Return SetError(2, 0, '')
For $ii = 0 To UBound($aSR) - 1
$sOut &= $aSR[$ii] & @CRLF
Next
If $FileName = '' Then Return $sOut
;~ If $FOMode <> 2 Then $FOMode = 1
$hFO = FileOpen($FileName, $FOMode)
FileWrite($hFO, $sOut)
FileClose($hFO)
EndFunc ;==>_StringKeyLine2File
|