关于正则表达式提取参数问题的疑问
问题描述如下:需要提取配置行中的部分数据,配置行例如:
2010-02-21 14:57:15 127.0.0.1 0.0.0.0 80 127.0.0.1 POST arg_post 30 log /index.asp
采用正则式“(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s+(.+)\s+(.+)\s+(.+)\s+(.+) \s+(.+)\s+(.+)\s+(.+)\s+(.+)\s+(.+)”,经过RXTest.exe(正则表达式测试工具)测试可以达到预期目的,如下图所示:
在VB6中采用如下代码,却无法达到上述效果,请高手指点。
程序代码:
Dim regEx As RegExp Dim Match As Match Dim Matches As MatchCollection Set regEx = New RegExp ' Create a regular expression. ' Set pattern. regEx.Pattern = "(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2})\s+(.+)\s+(.+)\s+(.+)\s+(.+)\s+(.+)\s+(.+)\s+(.+)\s+(.+)\s+(.+)" regEx.IgnoreCase = True ' Set case insensitivity. regEx.Global = True ' Set global applicability. Set Matches = regEx.Execute("2010-02-21 14:56:13 127.0.0.1 0.0.0.0 80 127.0.0.1 POST arg_post 30 log /index.asp") If Matches.Count = 10 Then attacklist.ListItems.Add , , Matches.Item(0) '时间 attacklist.ListItems.Add , , Matches.Item(1) '源IP attacklist.ListItems.Add , , Matches.Item(3) '目的端口 attacklist.ListItems.Add , , Matches.Item(7) '规则ID attacklist.ListItems.Add , , Matches.Item(8) '处理方式 attacklist.ListItems.Add , , Matches.Item(9) 'Url End If
附:在C++中采用std::tr1::regex_match我曾实现过类似的功能。