正则匹配到的关键词如何统计为数量[已解决]
本帖最后由 heroxianf 于 2017-2-24 11:03 编辑示例: ALSDKFJSJAALJDFLSJA;LFJDLSKJDGLKAJSD;LFJALDKSFJLAJSDFLKASJDFKAJDF
正则: AL|S|F
要如何才能统计AL、S、F到底有多少个呢?
在7楼 自己想了一个方法,大家一起讨论讨论,要是能做成函数返回个什么也好啊。
#include <array.au3>
Local $a = "0", $b = "0", $c = "0"
Local $Txt = "ALSDKFJSJAALJDFLSJA;LFJDLSKJDGLKAJSD;LFJALDKSFJLAJSDFLKASJDFKAJDF"
Local $Exp = "AL|S|F"
Local $Array_Txt = StringRegExp($Txt, $Exp, 3)
Local $Array_Exp = StringSplit($Exp, "|")
For $i = 0 To UBound($Array_Txt) - 1
If $Array_Txt[$i] = $Array_Exp Then
$a = $a + 1
ElseIf $Array_Txt[$i] = $Array_Exp Then
$b = $b + 1
ElseIf $Array_Txt[$i] = $Array_Exp Then
$c = $c + 1
EndIf
Next
MsgBox(0, "", $Array_Exp & ": " & $a & @CRLF _
& $Array_Exp & ": " & $b & @CRLF _
& $Array_Exp & ": " & $c) 回复 2# heroxianf
何必用for循环费那劲,这样可以不?
#include <array.au3>
Local $Txt = "ALSDKFJSJAALJDFLSJA;LFJDLSKJDGLKAJSD;LFJALDKSFJLAJSDFLKASJDFKAJDF"
$Array = StringRegExp($Txt, 'AL|S|F', 3)
$Array_AL = StringRegExp($Txt, 'AL', 3)
$Array_S = StringRegExp($Txt, 'S', 3)
$Array_F = StringRegExp($Txt, 'F', 3)
_ArrayDisplay($Array, 'AL:' & UBound($Array_AL) & ' ' & _
'S:' & UBound($Array_S) & ' ' & _
'F:' & UBound($Array_F)) 回复 3# haijie1223
发哥主要是考虑到正则增加关键词结果也自动增加的问题
现在是: AL|S|F
我改成: AL|S|F|测试
我想让结果自动也添加上“测试”这个结果。 加多少也是如此 回复 5# haijie1223
发哥,我是希望这样输出的,关键词随意增加后都可以提现出来。检查一下哇。
#include <array.au3>
Local $Txt = "ALSDKFJSJAALJDFLSJA;LFJDLSKJDGLKAJSD;LFJALDKSFJLAJSDFLKASJDFKAJDF are you ok?"
Local $Exp = "AL|S|F|L|are" ;随意增加关键字
Local $Out = ""
Local $Array_Exp = StringSplit($Exp, "|")
Local $Array
For $i = 1 To $Array_Exp
$Array[$i] = StringRegExp($Txt, $Array_Exp[$i], 3)
$Out &= $Array_Exp[$i] & ": " & UBound($Array[$i]) & @CRLF ;合并结果
Next
MsgBox(0, "", $Out) 回复 5# haijie1223
过滤掉关键词没有的~
#include <array.au3>
Local $Txt = "ALSDKFJSJAALJDFLSJA;LFJDLSKJDGLKAJSD;LFJALDKSFJLAJSDFLKASJDFKAJDF are you ok?"
Local $Exp = "AL|S|F|L|are|测试" ;随意增加关键字
Local $Out = ""
Local $Array_Exp = StringSplit($Exp, "|")
Local $Array
For $i = 1 To $Array_Exp
$Array[$i] = StringRegExp($Txt, $Array_Exp[$i], 3)
If UBound($Array[$i]) <> 0 Then ;过滤关键字是0的结果
$Out &= $Array_Exp[$i] & ": " & UBound($Array[$i]) & @CRLF ;合并结果
EndIf
Next
MsgBox(0, "", $Out)
页:
[1]