2011-01-25 5 views
1

저는 하루 종일 생각하고 있었고 해결할 수 없습니다.정규식을 사용하여 여러 쿼리 매개 변수 추출

아래 HTML이 있는데 "? imgurl ="과 일치하는 쿼리 매개 변수의 모든 값을 추출하려고합니다. 아무도 나를 위해 정규식이 나를 도울 수 있습니까?

</script></div><div id=nr_container><div id=center_col><div id=tbbcc><div id=tbbc style="background:#ebeff9;margin-bottom:4px;padding:8px;display:none"></div></div><div id=res class=med role=main><div id=topstuff></div><!--a--><h2 class=hd>Søgeresultater</h2><div id=ires><ol><script>google.isr.fillCanvas=function(i){var c=document.getElementById('cvs_'+i.id);try{c&&(c.getContext('2d').drawImage(i,0,0,c.offsetWidth,c.offsetHeight));}catch(e){c.style.display='none';i.style.display='block';}}</script><div id=rgsh_s></div><li><div id=rg><div id=rg_s><div id=rg_hp><a id=rg_hpl></a></div><div class=rg_h id=rg_h><div class=rg_hc><a class=rg_hl id=rg_hl><img class=rg_hi id=rg_hi></a><div class=std id=rg_hx><p class=rg_ht id=rg_ht><a id=rg_hta></a></p><p class=rg_hn id=rg_hn></p><p class=rg_hr><span id=rg_hr></span></p><p class=rg_ha><span id=rg_ha><a class=rg_hal id=rg_hals></a><span id=rg_has>&nbsp;&#8209;&nbsp;</span><a class=rg_hal id=rg_haln></a><span id=rg_has2>&nbsp;&#8209;&nbsp;</span><a class=rg_hal id=rg_halm></a></span></p></div></div></div><span class=rg_ctlv><ul class=rg_ul data-pg=1 data-cnt=44><li class=rg_li data-row=1 style="width:193px;height:145px" ><a class=rg_l style="width:193px;height:145px;margin-top:0px;margin-left:0px" href="/imgres?imgurl=http://www.eecs.berkeley.edu/~loarie/test.colors.gif&amp;imgrefurl=http://s1mon.smartlog.dk/test-post37556&amp;usg=__xdES-qA3W9Np6DMNDs0HPTe2Bn8=&amp;h=606&amp;w=807&amp;sz=18&amp;hl=da&amp;start=1&amp;zoom=1&amp;tbnid=sFzpf2rpdeVHLM:&amp;tbnh=107&amp;tbnw=143&amp;ei=Q9k-TYLkEob0swOzpdH0BA&amp;prev=/images%3Fq%3Dtest%26hl%3Dda%26safe%3Doff%26sa%3DG%26as_st%3Dy%26biw%3D1680%26bih%3D897%26tbs%3Disch:1&amp;itbs=1"><script>google.stb.csi.stTbn()</script><canvas id="cvs_sFzpf2rpdeVHLM:l" style="display:block" width=193 height=145></canvas><img class=rg_i id=sFzpf2rpdeVHLM:l height=145 width=193 style="width:193px;height:145px" onload="google.isr.fillCanvas(this);google.stb.csi.onTbn(1, this)"></a></li><li class=rg_li style="width:154px;height:145px" ><a class=rg_l style="width:160px;height:145px;margin-top:0px;margin-left:-2px" href="/imgres?imgurl=http://www.krymmel.dk/dev/media/.jkforum/test-pilot.png&amp;imgrefurl=http://www.krymmel.dk/dev/pages/forum.php&amp;usg=__a-KJQiDnKKy8LxlCV-d3XZpKGuw=&amp;h=327&amp;w=360&amp;sz=110&amp;hl=da&amp;start=2&amp;zoom=1&amp;tbnid=KLm4Rocmahp8wM:&amp;tbnh=110&amp;tbnw=121&amp;ei=Q9k-TYLkEob0swOzpdH0BA&amp;prev=/images%3Fq%3Dtest%26hl%3Dda%26safe%3Doff%26sa%3DG%26as_st%3Dy%26biw%3D1680%26bih%3D897%26tbs%3Disch:1&amp;itbs=1"><script>google.stb.csi.stTbn()</script><canvas id="cvs_KLm4Rocmahp8wM:l" style="display:block" width=160 height=145></canvas><img class=rg_i id=KLm4Rocmahp8wM:l height=145 width=160 style="width:160px;height:145px" onload="google.isr.fillCanvas(this);google.stb.csi.onTbn(1, this)"></a></li><li class=rg_li style="width:148px;height:145px" ><a class=rg_l style="width:148px;height:145px;margin-top:0px;margin-left:0px" href="/imgres?imgurl=http://colorvisiontesting.com/plate%2520with%25205.jpg&amp;imgrefurl=http://colorvisiontesting.com/ishihara.htm&amp;usg=__UfBI8sd8ldLjjiK3-7aGJo0zKy4=&amp;h=309&amp;w=315&amp;sz=142&amp;hl=da&amp;start=3&amp;zoom=1&amp;tbnid=2_UMDol8AQhejM:&amp;tbnh=115&amp;tbnw=117&amp;ei=Q9k-TYLkEob0swOzpdH0BA&amp;prev=/images%3Fq%3Dtest%26hl%3Dda%26safe%3Doff%26sa%3DG%26as_st%3Dy%26biw%3D1680%26bih%3D897%26tbs%3Disch:1&amp;itbs=1"><script>google.stb.csi.stTbn()</script><canvas id="cvs_2_UMDol8AQhejM:l" style="display:block" width=148 height=145></canvas><img class=rg_i id=2_UMDol8AQhejM:l height=145 width=148 style="width:148px;height:145px" onload="google.isr.fillCanvas(this);google.stb.csi.onTbn(1, this)"></a></li><li class=rg_li style="width:193px;height:145px" ><a class=rg_l style="width:193px;height:145px;margin-top:0px;margin-left:0px" href="/imgres?imgurl=http://pun.org/josh/archives/04.10.01.GlobalTest-X.gif&amp;imgrefurl=http://hovedstaden.inetgiant.dk/fredensborg/AdDetails/test/3187460&amp;usg=___4P_UDkeMuovXCIjq-PY9WhG1Vw=&amp;h=391&amp;w=520&amp;sz=44&amp;hl=da&amp;start=4&amp;zoom=1&amp;tbnid=l15zkNo3p4iYcM:&amp;tbnh=99&amp;tbnw=131&amp;ei=Q9k-TYLkEob0swOzpdH0BA&amp;prev=/images%3Fq%3Dtest%26hl%3Dda%26safe%3Doff%26sa%3DG%26as_st%3Dy%26biw%3D1680%26bih%3D897%26tbs%3Disch:1&amp;itbs=1"><script>google.stb.csi.stTbn()</script><canvas id="cvs_l15zkNo3p4iYcM:l" style="display:block" width=193 height=145></canvas><img class=rg_i id=l15zkNo3p4iYcM:l height=145 width=193 style="width:193px;height:145px" onload="google.isr.fillCanvas(this);google.stb.csi.onTbn(1, this)"></a></li><li class=rg_li style="width:193px;height:145px" ><a class=rg_l style="width:193px;height:139px;margin-top:3px;margin-left:0px" href="/imgres?imgurl=http://www.daimi.au.dk/~rvinge/Test_daimi.jpg&amp;imgrefurl=http://www.daimi.au.dk/~rvinge/Hot.list.html&amp;usg=__ofrC4G4FpZgXi95enpnIG4Wpdlg=&amp;h=881&amp;w=1223&amp;sz=228&amp;hl=da&amp;start=5&amp;zoom=1&amp;tbnid=WDreIpjcKhg13M:&amp;tbnh=108&amp;tbnw=150&amp;ei=Q9k-TYLkEob0swOzpdH0BA&amp;prev=/images%3Fq%3Dtest%26hl%3Dda%26safe%3Doff%26sa%3DG%26as_st%3Dy%26biw%3D1680%26bih%3D897%26tbs%3Disch:1&amp;itbs=1"><script>google.stb.csi.stTbn()</script><canvas id="cvs_WDreIpjcKhg13M:l" style="display:block" width=193 height=139></canvas><img class=rg_i id=WDreIpjcKhg13M:l height=139 width=193 style="width:193px;height:139px" onload="google.isr.fillCanvas(this);google.stb.csi.onTbn(1, this)"></a></li><li class=rg_li style="width:143px;height:145px" ><a class=rg_l style="width:145px;height:145px;margin-top:0px;margin-left:0px" href="/imgres?imgurl=http://www.textually.org/tv/archives/images/set3/test-pattern-clock_4767.jpg&amp;imgrefurl=http://hovedstaden.inetgiant.dk/fredensborg/AdDetails/test/3187460&amp;usg=__BFaPejcst7ygnE72uTI6sJKxmIk=&amp;h=308&amp;w=307&amp;sz=18&amp;hl=da&amp;start=6&amp;zoom=1&amp;tbnid=m1QYUHLkZ-mXCM:&amp;tbnh=117&amp;tbnw=117&amp;ei=Q9k-TYLkEob0swOzpdH0BA&amp;prev=/images%3Fq%3Dtest%26hl%3Dda%26safe%3Doff%26sa%3DG%26as_st%3Dy%26biw%3D1680%26bih%3D897%26tbs%3Disch:1&amp;itbs=1"><script>google.stb.csi.stTbn()</script><canvas id="cvs_m1QYUHLkZ-mXCM:l" style="display:block" width=145 height=145></canvas><img class=rg_i id=m1QYUHLkZ-mXCM:l height=145 width=145 style="width:145px;height:145px" onload="google.isr.fillCanvas(this);google.stb.csi.onTbn(1, this)"></a></li><li class=rg_li style="width:118px;height:145px" ><a class=rg_l style="width:118px;height:145px;margin-top:0px;margin-left:0px" href="/imgres?imgurl=http://imgs.xkcd.com/comics/turing_test.png&amp;imgrefurl=http://xkcd.com/329/&amp;usg=__DdATXOcoguD2UbYUMs_iwi4r54I=&amp;h=394&amp;w=320&amp;sz=22&amp;hl=da&amp;start=7&amp;zoom=1&amp;tbnid=UeYWZFjYErEM6M:&amp;tbnh=124&amp;tbnw=101&amp;ei=Q9k-TYLkEob0swOzpdH0BA&amp;prev=/images%3Fq%3Dtest%26hl%3Dda%26safe%3Doff%26sa%3DG%26as_st%3Dy%26biw%3D1680%26bih%3D897%26tbs%3Disch:1&amp;itbs=1"><script>google.stb.csi.stTbn()</script><canvas id="cvs_UeYWZFjYErEM6M:l" style="display:block" width=118 height=145></canvas><img class=rg_i id=UeYWZFjYErEM6M:l height=145 width=118 style="width:118px;height:145px" onload="google.isr.fillCanvas(this);google.stb.csi.onTbn(1, this)"></a></li><li class=rg_li style="width:133px;height:145px" ><a class=rg_l style="width:149px;height:145px;margin-top:0px;margin-left:-4px" href="/imgres?imgurl=http://thomasdamgaard.dk/blog/images/test01.jpg&amp;imgrefurl=http://thomasdamgaard.dk/blog/test-skilt-pa-motorvejen&amp;usg=__quqWeHGs6OFAggLm5DBauetlRQU=&amp;h=487&amp;w=500&amp;sz=22&amp;hl=da&amp;start=8&amp;zoom=1&amp;tbnid=HwAHMYrtavz5IM:&amp;tbnh=127&amp;tbnw=130&amp;ei=Q9k-TYLkEob0swOzpdH0BA&amp;prev=/images%3Fq%3Dtest%26hl%3Dda%26safe%3Doff%26sa%3DG%26as_st%3Dy%26biw%3D1680%26bih%3D897%26tbs%3Disch:1&amp;itbs=1"><script>google.stb.csi.stTbn()</script><canvas id="cvs_HwAHMYrtavz5IM:l" style="display:block" width=149 height=145></canvas><img class=rg_i id=HwAHMYrtavz5IM:l height=145 width=149 style="width:149px;height:145px" onload="google.isr.fillCanvas(this);google.stb.csi.onTbn(1, this)"></a></li><li class=rg_li style="width:100px;height:145px" ><a class=rg_l style="width:102px;height:145px;margin-top:0px;margin-left:0px" href="/imgres?imgurl=http://www.ct4me.net/images/dmbtest.gif 
+0

사용중인 언어/플랫폼은 무엇입니까? – Oded

+3

HTML을 재구성하여보다 읽기 쉽고 추출한 것으로 예상되는 결과를 얻을 수 있습니다. – mhitza

답변

1

사람들이 너무 빨리 HTML을 구문 분석하기 위해 정규 표현식을 사용하지 말라는 것을 저를 초조하게합니다. 어쨌든 HTML을 파싱하지 않습니다. Html Agility Pack을 사용하여 HTML에서 URL을 추출하는 경우에도 각 검색어 문자열에서 imgurl 매개 변수를 추출해야합니다.

정규식은 쿼리 문자열에서 매개 변수를 추출하기위한 완벽한이며, 이것은 당신이 원하는 것을 할 것입니다 :

string input = "your big HTML string"; 
MatchCollection matches = Regex.Matches(
    input, 
    @"(?<=[?&]imgurl=)[^&#'"]*", 
    RegexOptions.IgnoreCase // remove this if you don't want to ignore case in "imgurl" 
); 

실제로 구문 분석 HTML의 HTML 민첩성 팩을 사용하는 모든 해요,하지만 당신은 그냥 원하는 경우 더 큰 문자열에서 잘 정의 된 패턴에 맞는 문자열을 제거하려면 정규식보다 더 나은 도구가 필요하지 않습니다. HTML 태그를 구문 분석하기 위해 정규 표현식을 사용하는 것이 좋지 않은 이유는 HTML이 안정적으로 구조화되어 있지 않기 때문입니다. URL의 쿼리 문자열은 특정 형식이어야하므로 regex를 사용하는 것이 안전합니다.

+0

이것은 처음에 내가 찾고 있던 간단한 스트립 이미지 URL입니다. Oded의 답변에 대한 내 의견을 고려하면 자바 스크립트이므로 RegEx가 정상적으로 작동합니다. 프로토 타입이므로 실용적인 솔루션을 찾고있었습니다. – esbenr

1

HTML을 구문 분석하기 위해 정규식을 사용하지 마십시오.

왜 강력한 데모인지는 here을 참조하십시오.

플랫폼/언어로 HTML 구문 분석기를 사용하십시오.


편집 :

당신은 C#을 사용 표시된 것처럼

, 나는 HTML Agility Pack을 사용하는 것이 좋습니다 - 널리 사용되며 된 XmlDocument 같이 XPath를 함께 조회 할 수 있습니다.

귀하의 특별한 필요에 따라, 나는 모든 링크를 얻고 각각의 사용에 대해 string.Split을 사용하여 필요한 쿼리 문자열 매개 변수를 얻을 수 있습니다.

+0

Okey. 그것이 말했다. 나는 C#을 사용하고 있으므로 파서의 개요를 던져 줄 수 있습니까? – esbenr

+0

[HTML 민첩성 팩] (http://htmlagilitypack.codeplex.com/)을 사용하여 URL을 추출한 다음 'string.Split'을 사용하여 필요한 매개 변수를 가져옵니다. – Oded

+0

해당 URL 중 일부가 HTML의 자바 스크립트 코드의 일부일 경우 어떻게됩니까? –

관련 문제