文本正则提取-帮忙简化一下代码[已解决]
本帖最后由 仙乃日 于 2022-4-10 20:30 编辑#include <Array.au3>
Local $sSource = _
'2022 MAR 05 08:47:54' & @CRLF & _
' NAME:ML235004' & @CRLF & _
' ID:0-0001 (00000307)' & @CRLF & _
' LEU - neg' & @CRLF & _
' NIT - neg' & @CRLF & _
' URO - <=0.2mg/dL' & @CRLF & _
' PRO - 5mg/dL' & @CRLF & _
' PH 5.5 ' & @CRLF & _
' BLO - neg' & @CRLF & _
' SG 1.025 ' & @CRLF & _
' KET +- 5mg/dL' & @CRLF & _
'*BIL 2+ 2mg/dL' & @CRLF & _
' GLU - neg' & @CRLF & _
' ASC - neg'
Local $aSRE = StringRegExp($sSource, ' NAME:(?<grp0>[^\n]+)', 3)
If Not @Error Then MsgBox(0, '匹配数量: ' & UBound($aSRE), '其中元素为: ' & $aSRE)
_ArrayDisplay($aSRE, UBound($aSRE))
Local $sSource = _
'2022 MAR 05 08:47:54' & @CRLF & _
' NAME:ML235005' & @CRLF & _
' ID:0-0001 (00000307)' & @CRLF & _
' LEU - neg' & @CRLF & _
' NIT - neg' & @CRLF & _
' URO - <=0.2mg/dL' & @CRLF & _
' PRO +- neg' & @CRLF & _
' PH 5.5 ' & @CRLF & _
' BLO - neg' & @CRLF & _
' SG 1.025 ' & @CRLF & _
' KET +- 5mg/dL' & @CRLF & _
'*BIL 2+ 2mg/dL' & @CRLF & _
' GLU - neg' & @CRLF & _
' ASC - neg'
Local $aSRE = StringRegExp($sSource, 'PRO\h*([\-|\+|\d]*)\h*', 3)
If Not @Error Then MsgBox(0, '匹配数量: ' & UBound($aSRE), '其中元素为: ' & $aSRE)
_ArrayDisplay($aSRE, UBound($aSRE))
需要将文本转换成INI文件,使用NAME:后边的条码作为节名.
PRO=+-
帮忙简化一下代码
感谢各位的帮忙 ### 友情提示:本脚本由 Au3.REHelper 于 2022/04/09 10:36 自动生成,不保证其正确性,请自行测试 ###
#include <Array.au3>
Local $sSource = _
'2022 MAR 05 08:47:54' & @CR & @CRLF & _
' NAME:ML235004' & @CR & @CRLF & _
' ID:0-0001 (00000307)' & @CR & @CRLF & _
' LEU - neg' & @CR & @CRLF & _
' NIT - neg' & @CR & @CRLF & _
' URO - <=0.2mg/dL' & @CR & @CRLF & _
' PRO - 5mg/dL' & @CR & @CRLF & _
' PH 5.5 ' & @CR & @CRLF & _
' BLO - neg' & @CR & @CRLF & _
' SG 1.025 ' & @CR & @CRLF & _
' KET +- 5mg/dL' & @CR & @CRLF & _
'*BIL 2+ 2mg/dL' & @CR & @CRLF & _
' GLU - neg' & @CR & @CRLF & _
' ASC - neg'
;~MsgBox(0, '源字符串', $sSource)
Local $aSRE = StringRegExp($sSource, '(?s)NAME:(\w+).+PRO\h*([+-]+)', 3)
If Not @Error Then MsgBox(0, '匹配数量: ' & UBound($aSRE), '其中元素为: ' & $aSRE)
_ArrayDisplay($aSRE, UBound($aSRE))
也可以替换模式
Local $sSRERe = StringRegExpReplace($sSource, '(?si).+?NAME:(\V+).+?(PRO)\h+([-+\d]+).+', '[$1]' & @CRLF & '$2=$3') 我来凑个热闹
Local $aSRE = StringRegExp($sSource, '(?<=PRO|NAME:)\h*(\S+)\h*', 3) 传说中的"仙乃日"???
页:
[1]