问题描述
$WEB_URL_PATTERN = /web_url[s]*(.*)[s]*;/murl_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大家好我想用$WEB_URL_PATTERN去匹配url_string中的web_url函数,由于第3个web_url被注释掉了,我并不想让它也出现在结果中,请问我该如何修改正则表达式,让正则表达式能够正常处理这种情况,多谢! 问题补充:jim.jin 写道
解决方案
Ruby 和Tcl 甚至根本就不支持逆序环视结构
解决方案二:
引用问题补充:引用可以在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 look-behind: /(?<![s]*/*)web_url[s]*(.*?)[s]*;(?![s]**/)/m (SyntaxError) 由于ruby1.87的REGEX引擎不支持逆序环视,1.92才支持 估计用http://www.rubular.com/是测试不了了原因不在这里,因为这里有贪婪匹配。只要稍微修改下就可以了。这样:加上一个+?(web_url[s]*(.*?)[s]*;)+?(?!([s]**/))测试stringweb_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的环视没有用过。
解决方案三:
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);
解决方案四:
刚代码有点问题。这样写:web_url[s]*(.*)[s]*;(?!((s)**/))可以在http://www.rubular.com/测试下。patten = web_url[s]*(.*)[s]*;(?!((s)**/))args = mstr = 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); */
解决方案五:
你的代码贴出来报错。用前瞻就可以:$WEB_URL_PATTERN = /(?!/*s)web_url[s]*(.*)[s]*;/m
解决方案六:
将/* */ 中的内容都去掉在做匹配:url_string.gsub(//*.*?*/im, '').scan($WEB_URL_PATTERN)