Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python正则表达式示例_Python - Fatal编程技术网

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
不做任何替换,它只查找与正则表达式匹配的项。。。