python正则表达式示例
我创建了一个正则表达式来查找匹配的字符串,如下所示python正则表达式示例,python,Python,我创建了一个正则表达式来查找匹配的字符串,如下所示 window.location="process.php?file=Generics/index.nsp"; location.href = "http://www.foo.com"; location.href="http://www.foo.com"; window.location = "http://www.foo.com"; window.location.href="http://www.foo.com"; location.rep
window.location="process.php?file=Generics/index.nsp";
location.href = "http://www.foo.com";
location.href="http://www.foo.com";
window.location = "http://www.foo.com";
window.location.href="http://www.foo.com";
location.replace ("http://www.foo.com");
location.replace( "http://www.foo.com" ) ;
location.assign ("http://www.foo.com");
window.location= var +"process.php?file=Generics/index.nsp";
window.location.href = path + "login" + ".html";
window.location.href = path + featureId + ".html";
window.location.href = "/" + "online-banking" + ".html";
window.location.href = path + featureName +".html";
window.location.href = link.page;
window.location.href = path + link.page + ".html";'
['window.location="process.php?file=Generics/index.nsp"','location.href = "http://www.foo.com"','window.location = "http://www.foo.com"','window.location.href="http://www.foo.com"',..]
我在正则表达式测试页面中检查了这个表达式是否正常工作
然而,当我执行程序时,我无法得到正确的结果
# -*- coding: utf-8 -*-
import re
url_reg= re.compile('(location\.(href|assign|replace)|window\.location(\.href|))\s*(=|\()+.*(;|$)')
test = '123123gjh123\
2135115123\
window.location="process.php?file=Generics/index.nsp";\
location.href = "http://www.foo.com";\
location.href="http://www.foo.com";\
window.location = "http://www.foo.com";\
window.location.href="http://www.foo.com";\
location.replace ("http://www.foo.com");\
location.replace( "http://www.foo.com" ) ; \
location.assign ("http://www.foo.com");\
window.location= var +"process.php?file=Generics/index.nsp";\
window.location.href = path + "login" + ".html";\
window.location.href = path + featureId + ".html";\
window.location.href = "/" + "online-banking" + ".html";\
window.location.href = path + featureName +".html";\
window.location.href = link.page;\
window.location.href = path + link.page + ".html";'
print url_reg.findall(test)
结果:
[('window.location', '', '', '=', '')]
我想得到如下结果值
window.location="process.php?file=Generics/index.nsp";
location.href = "http://www.foo.com";
location.href="http://www.foo.com";
window.location = "http://www.foo.com";
window.location.href="http://www.foo.com";
location.replace ("http://www.foo.com");
location.replace( "http://www.foo.com" ) ;
location.assign ("http://www.foo.com");
window.location= var +"process.php?file=Generics/index.nsp";
window.location.href = path + "login" + ".html";
window.location.href = path + featureId + ".html";
window.location.href = "/" + "online-banking" + ".html";
window.location.href = path + featureName +".html";
window.location.href = link.page;
window.location.href = path + link.page + ".html";'
['window.location="process.php?file=Generics/index.nsp"','location.href = "http://www.foo.com"','window.location = "http://www.foo.com"','window.location.href="http://www.foo.com"',..]
请给我一些建议。这是你的目的吗?我刚刚添加了
r'
和outter parentheis
import re
url_reg= re.compile(r'((location\.(href|assign|replace)|window\.location(\.href)?)\s*(=|\()+.*(;|$))')
test = 'blahblah...'
print map(lambda x: x[0], url_reg.findall(test))
['window.location=“process.php?file=Generics/index.nsp”;location.href=”http://www.foo.com“location.href=”http://www.foo.com“window.location=”http://www.foo.com“window.location.href=”http://www.foo.com“location.replace(”http://www.foo.com“”;位置。替换(“”)http://www.foo.com“”;location.assign(“”)http://www.foo.com");window.location=var+“process.php?file=Generics/index.nsp”;window.location.href=path+“login”+“.html”;window.location.href=path+featureId+”.html;window.location.href=“/”+“网上银行”+“.html”;window.location.href=path+featureName+“.html”;window.location.href=link.page;window.location.href=path+link.page+“.html”;']
findall
不做任何替换,它只查找与正则表达式匹配的项。。。