正则提取内容并计算【已解决】
本帖最后由 3131210 于 2017-4-30 00:37 编辑比如说 以下内容在剪贴板或者在一个文本里面,我想正则提取数据【存款】一共有多少 【提款】一共有多少 负盈利(存款-提款)有多少 怎么写代码呢?
存款 test TB 20
提款 test TB 200
存款 test TB 50
存款 test TB 30
存款 test TB 50
存款 test TB 50
存款 test TB 20
存款 test TB 20
存款 test TB 50
提款 test TB 100
存款 test TB 30
#include <ARRAY.AU3>
$string = _
'存款 test TB 20' & @CRLF & _
'提款 test TB 200' & @CRLF & _
'存款 test TB 50' & @CRLF & _
'存款 test TB 30' & @CRLF & _
'存款 test TB 50' & @CRLF & _
'存款 test TB 50' & @CRLF & _
'存款 test TB 20' & @CRLF & _
'存款 test TB 20' & @CRLF & _
'存款 test TB 50' & @CRLF & _
'提款 test TB 100' & @CRLF & _
'存款 test TB 30'
Local $ARRAY = StringRegExp($string, '\S+', 3)
Local $ret = 0
For $I = 0 To UBound($ARRAY) - 1 Step 4
If StringInStr($ARRAY[$I], '提') Then $ARRAY[$I + 3] = -1 * $ARRAY[$I + 3]
$ret += $ARRAY[$I + 3]
Next
MsgBox(0, '合计', $ret) #include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $jishu,$chun,$qukuan
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 438, 192, 124)
$Label1 = GUICtrlCreateLabel("存款:", 128, 104, 136, 17)
$Label2 = GUICtrlCreateLabel("提款:", 128, 152, 136, 17)
$Label3 = GUICtrlCreateLabel("负盈利:", 128, 208, 156, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$str="存款 test TB 20"&@CRLF & _
"提款 test TB 200"&@CRLF & _
"存款 test TB 50"&@CRLF & _
"存款 test TB 30"&@CRLF & _
"存款 test TB 50"&@CRLF & _
"存款 test TB 50"&@CRLF & _
"存款 test TB 20"&@CRLF & _
"存款 test TB 20"&@CRLF & _
"存款 test TB 50"&@CRLF & _
"提款 test TB 100"&@CRLF & _
"存款 test TB 30"
$str1 = StringRegExp($str, '+',3)
If Not @error Then
ReDim $jishu
For $i = 0 To UBound($jishu) - 1
$jishu[$i] = $str1[$i * 4] ;存取款
$jishu[$i] = $str1[$i * 4+3] ;发生的金额
If StringInStr($jishu[$i], "存") Then
$chun += $jishu[$i]
Else
$qukuan += $jishu[$i]
EndIf
Next
GUICtrlSetData($Label1, "存款:" & $chun)
GUICtrlSetData($Label2, "提款:" & $qukuan)
GUICtrlSetData($Label3, "负盈利:" & $chun - $qukuan)
EndIf
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
WEnd感谢群友fuldho与ACN~seniors数组相加的帮助! 非常感谢。
页:
[1]