ruby中正则表达式的使用

[code="ruby"]$WEB_URL_PATTERN = /web_url[\s]*(.*)[\s]*;/m

url_string = <<'EOF'
web_url("crossdomain.xml",
"URL=http://{RASiteURL}/crossdomain.xml",
"TargetFrame=",
"Resource=0",
"RecContentType=text/xml",
"Referer=",
"Snapshot=t10.inf",
"Mode=HTML",
LAST);

web_url("DashboardServlet",
    "URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",
    "TargetFrame=",
    "Resource=0",
    "RecContentType=text/xml",
    "Referer=http://{WebSiteURL}/lo-web/flex/main.swf",
    "Snapshot=t11.inf",
    "Mode=HTML",
    LAST);

/*
web_url("DashboardServlet_2",
    "URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",
    "TargetFrame=",
    "Resource=0",
    "RecContentType=text/xml",
    "Referer=http://{WebSiteURL}/lo-web/flex/main.swf",
    "Snapshot=t12.inf",
    "Mode=HTML",
    LAST);
*/

EOF[/code]

大家好

我想用[color=orange]$WEB_URL_PATTERN[/color]去匹配[color=orange]url_string[/color]中的[color=orange]web_url[/color]函数,由于第3个[color=orange]web_url[/color]被注释掉了,我并不想让它也出现在结果中,请问我该[b]如何修改正则表达式[/b],让正则表达式能够正常处理这种情况,多谢!

Ruby 和Tcl 甚至根本就不支持逆序环视结构

将/* / 中的内容都去掉在做匹配:
[code="ruby"]
url_string.gsub(/\/\
.*?*\/im, '').scan($WEB_URL_PATTERN)
[/code]

你的代码贴出来报错。
用前瞻就可以:
[code="ruby"]
$WEB_URL_PATTERN = /(?!\/*\s)web_url[\s]*(.*)[\s]*;/m
[/code]

刚代码有点问题。这样写:

[code="java"]web_url[\s]*(.*)[\s]*;(?!((\s)**\/))[/code]

可以在[url]http://www.rubular.com/[/url]测试下。

patten = web_url[\s]*(.*)[\s]*;(?!((\s)**\/))

args = m

str = web_url("crossdomain.xml",

"URL=http://{RASiteURL}/crossdomain.xml",

"TargetFrame=",

"Resource=0",

"RecContentType=text/xml",

"Referer=",

"Snapshot=t10.inf",

"Mode=HTML",

LAST);

web_url("DashboardServlet",  
    "URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",  
    "TargetFrame=",  
    "Resource=0",  
    "RecContentType=text/xml",  
    "Referer=http://{WebSiteURL}/lo-web/flex/main.swf",  
    "Snapshot=t11.inf",  
    "Mode=HTML",  
    LAST);  

/*  
web_url("DashboardServlet_2",  
    "URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",  
    "TargetFrame=",  
    "Resource=0",  
    "RecContentType=text/xml",  
    "Referer=http://{WebSiteURL}/lo-web/flex/main.swf",  
    "Snapshot=t12.inf",  
    "Mode=HTML",  
    LAST);  
*/  

result =

web_url("crossdomain.xml",

"URL=http://{RASiteURL}/crossdomain.xml",

"TargetFrame=",

"Resource=0",

"RecContentType=text/xml",

"Referer=",

"Snapshot=t10.inf",

"Mode=HTML",

LAST);

web_url("DashboardServlet",  
    "URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",  
    "TargetFrame=",  
    "Resource=0",  
    "RecContentType=text/xml",  
    "Referer=http://{WebSiteURL}/lo-web/flex/main.swf",  
    "Snapshot=t11.inf",  
    "Mode=HTML",  
    LAST);

[quote]问题补充:
引用
可以在http://www.rubular.com/测试下。

patten = web_url[\s]*(.*)[\s]*;(?!((\s)**\/))

非常感谢!

但还是有一点儿问题

如果是这种情况,就不行了

str = web_url("crossdomain.xml",
"URL=http://{RASiteURL}/crossdomain.xml",
"Resource=0",
"RecContentType=text/xml",
"Mode=HTML",
LAST);
/*
web_url("DashboardServlet",
"URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",
"Resource=0",
"RecContentType=text/xml",
"Mode=HTML",
LAST);
*/
web_url("DashboardServlet_2",
"URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",
"Resource=0",
"RecContentType=text/xml",
"Mode=HTML",
LAST);

所以我按你的思路,写成这样:
$WEB_URL_PATTERN = /(?<![\s]*\/*)web_url[\s]*(.*?)[\s]*;(?![\s]**\/)/m

我加了个否定逆序环视上去,可为啥就不行呢?

运行的时候ruby1.92报错
invalid pattern in [color=blue]look-behind: /(?<![\s]*\/*)web_url[\s]*(.*?)[\s]*;(?![\s]**\/)/m (SyntaxError)[/color]

由于ruby1.87的REGEX引擎不支持逆序环视,1.92才支持
估计用http://www.rubular.com/是测试不了了[/quote]

原因不在这里,因为这里有贪婪匹配。只要稍微修改下就可以了。

这样:加上一个+?

code="ruby"+?(?!([\s]**\/))[/code]

测试string

web_url("crossdomain.xml",
"URL=http://{RASiteURL}/crossdomain.xml",
"Resource=0",
"RecContentType=text/xml",
"Mode=HTML",
LAST);
/*
web_url("DashboardServlet",
"URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",
"Resource=0",
"RecContentType=text/xml",
"Mode=HTML",
LAST);
/
web_url("DashboardServlet_2",
"URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",
"Resource=0",
"RecContentType=text/xml",
"Mode=HTML",
LAST);
/

web_url("DashboardServlet",
"URL=http://{WebSiteURL}/fbi.dashboard.ui/bridge/DashboardServlet",
"Resource=0",
"RecContentType=text/xml",
"Mode=HTML",
LAST);
*/

还用这个网站可以测试的。
ruby的环视没有用过。