找回密码
 加入
搜索
查看: 4396|回复: 11

[AU3基础] [已解决]正则:精确匹配给出文本的每一行?

  [复制链接]
发表于 2011-6-23 16:29:43 | 显示全部楼层 |阅读模式
本帖最后由 newuser 于 2011-6-27 09:38 编辑
this is a red fox
this is a blue firefox
this is a pig
a red fox
以上是文本,看资料给出的答案是:^(this is )?a (red fox|blue firefox|pig)$,但测试不行?
我用 ^(?:this\sis\s)a\s(?:red\sfox)|(?:blue\sfirefox)|(?:pig) 试图精确匹配每一行,但错误,卡在了 |  多条件选择上,把他们用[]或()都没有结果?
大家有好的办法吗?
发表于 2011-6-23 18:00:34 | 显示全部楼层
回复 1# newuser

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-6-23 22:31:18 | 显示全部楼层
唉,界面在 3mile 那按钮文字没显示完整,以后再抽空改改……
发表于 2011-6-23 22:39:51 | 显示全部楼层
回复 3# afan
哦,不是AFAN兄的原因,是我的分辨率设得是1920*1200,设了大字体的原因
 楼主| 发表于 2011-6-24 08:53:16 | 显示全部楼层
回复 4# 3mile
(?:this is )?a (?:red fox|blue firefox|pig)
我是模式用错了,不过看资料还不理解:
0 返回 1(匹配) 或 0(不匹配)
1 返回匹配项目的数组.
2 返回包括完整匹配的数组.(Perl/ PHP 样式).
3 返回全局匹配的数组.
4 返回包括完整匹配(Perl/ PHP 样式)和全局匹配的数组.
0好理解,就是看 表达式 是否与字符串中的内容能否匹配
1返回匹配项目的数组,但测试时他只返回第一个匹配的数组this is a red fox,难道1应该是只要有返回第1个匹配的数组就停止继续匹配操作吗?
2返回完整的匹配数组?和用1测试结果一样,完整的意思是什么?
3返回全局匹配的数组,和我想要的一样,精确匹配出每一行
4感觉全局匹配已经包括2了,我的理解可能局限我的问题了?
 楼主| 发表于 2011-6-24 08:53:24 | 显示全部楼层
回复 3# afan
(?:this is )?a (?:red fox|blue firefox|pig)
我是模式用错了,不过看资料还不理解:
0 返回 1(匹配) 或 0(不匹配)
1 返回匹配项目的数组.
2 返回包括完整匹配的数组.(Perl/ PHP 样式).
3 返回全局匹配的数组.
4 返回包括完整匹配(Perl/ PHP 样式)和全局匹配的数组.
0好理解,就是看 表达式 是否与字符串中的内容能否匹配
1返回匹配项目的数组,但测试时他只返回第一个匹配的数组this is a red fox,难道1应该是只要有返回第1个匹配的数组就停止继续匹配操作吗?
2返回完整的匹配数组?和用1测试结果一样,完整的意思是什么?
3返回全局匹配的数组,和我想要的一样,精确匹配出每一行
4感觉全局匹配已经包括2了,我的理解可能局限我的问题了?
 楼主| 发表于 2011-6-24 09:18:17 | 显示全部楼层
本帖最后由 newuser 于 2011-6-24 09:20 编辑

回复 4# 3mile
再请教一个问题:
在reghelper工具测试正常,如图:

在AU3测试却没有显示任何结果,代码如下:
#include <array.au3>
Local  $str="this is a red fox"&@crlf& _
            "this is a blue firefox"&@CRLF& _
                        "this is a pig"&@crlf& _
                        "a red fox"
Local  $result=StringRegExp($str,"(?m)(?:^this is )?a (?:red fox|blue firefox|pig)",4);返回完整匹配的数组和全局匹配的数组
_ArrayDisplay($result )            
为什么?

第2个问题,如果我想加$,加在哪?测试时,加在几个位置都失败!!!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2011-6-24 10:40:48 | 显示全部楼层
本帖最后由 3mile 于 2011-6-24 10:45 编辑

回复 7# newuser
#include <array.au3>
Local  $str="this is a red fox"&@crlf& _
            "this is a blue firefox"&@CRLF& _
                        "this is a pig"&@crlf& _
                        "a red fox"
Local  $result=StringRegExp($str,"(?m)(?:^this is )?a (?:red fox|blue firefox|pig)",4);返回完整匹配的数组和全局匹配的数组
;~ for $temp in $result
;~         _ArrayDisplay($temp )
;~ Next

for $i=0 to UBound($result)-1
        _ArrayDisplay($result[$i])
Next


#include <array.au3>
Local  $str="this is a red fox"&@crlf& _
            "this is a blue firefox"&@CRLF& _
                        "this is a pig"&@crlf& _
                        "a red fox"
Local  $result=StringRegExp($str,"(this is )?a (red fox|blue firefox|pig)",4);返回完整匹配的数组和全局匹配的数组
;~ for $temp in $result
;~         _ArrayDisplay($temp )
;~ Next

for $i=0 to UBound($result)-1
        _ArrayDisplay($result[$i])
Next


#include <array.au3>
Local  $str="this is a red fox"&@crlf& _
            "this is a blue firefox"&@CRLF& _
                        "this is a pig"&@crlf& _
                        "a red fox"
Local  $result=StringRegExp($str,"((?m)(^this is )?a (red fox|blue firefox|pig))",3);返回完整匹配的数组和全局匹配的数组
_ArrayDisplay($result)
local $str_result
for $i=0 to UBound($result)-1 step 3;共3组,只取第一组
        $str_result&=$result[$i]&@CRLF
Next
msgbox(0,0,$str_result)
 楼主| 发表于 2011-6-24 11:03:57 | 显示全部楼层
回复 8# 3mile
非常感谢,用3也得到匹配的全局数组,用_arraydisplay可以显示,可用到4怎么还得靠for循环帮忙,对完整数组匹配和全局匹配数组概念还是模糊?
发表于 2011-6-24 21:13:03 | 显示全部楼层
回复 9# newuser


    标志3可以,不用循环~
#include <Array.au3>
Local $Str = _
                'this is a red fox' & @CRLF & _
                'this is a blue firefox' & @CRLF & _
                'this is a pig' & @CRLF & _
                'a red fox'
Local $Test = StringRegExp($str, '(?m)(?:^this is )?a (?:red|blue|pig)\V*', 3)
_ArrayDisplay($Test, UBound($Test))
发表于 2011-6-24 21:14:26 | 显示全部楼层
回复  afan
哦,不是AFAN兄的原因,是我的分辨率设得是1920*1200,设了大字体的原因
3mile 发表于 2011-6-23 22:39



    哦是吗~ 差点就要瞎折腾了…
发表于 2011-11-27 04:57:46 | 显示全部楼层
本帖最后由 lixiaolong 于 2011-11-27 04:58 编辑

回复 1# newuser

(?m)^.*?$
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-21 04:37 , Processed in 0.084204 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表