Mansoor Akram Mansoor Akram - 4 months ago 7
HTML Question

Regex not working as required

Here is my HTML code:

<ul class="hide menuSearchType">
<li><a href="../../dynamic/city_select.aspx">Search by city</a></li>
<li><a href="../../searchbyphone.aspx">Search by phone</a></li>
<li><a href="../searchbyaddress.aspx">Search by address</a></li>
<li><a href="../searchbybrand.aspx">Search by brand</a></li>
<li><a href="/advertisement-center/">Advertise with us</a></li>
<li><a href="/advertisement-center/">Advertise with us</a></li>
<li><a href="//fonts.googleapis.com/css?family=Open+Sans">Find a Person</a></li>
<li><a href="//fonts.googleapis.com/css?family=Open+Sans">Find a Person</a></li>
<li><a href="dynamic/city_select.aspx">Search by city</a></li>
<li><a href="searchbybrand.aspx">Search by brand</a></li>
</ul>


Here is my Python code:

import re, os
from urllib.parse import urlparse

url = "http://www.phonebook.com.pk/dynamic/search.aspx?searchtype=cat&class_id=2566"

path = urlparse(url)
lpath = os.path.dirname(path.path)

html = u"<ul class=\"hide menuSearchType\">\n <li><a href=\"../../dynamic/city_select.aspx\">Search by city</a></li>\n <li><a href=\"../../searchbyphone.aspx\">Search by phone</a></li>\n <li><a href=\"../searchbyaddress.aspx\">Search by address</a></li>\n <li><a href=\"../searchbybrand.aspx\">Search by brand</a></li>\n <li><a href=\"/advertisement-center/\">Advertise with us</a></li>\n <li><a href=\"/advertisement-center/\">Advertise with us</a></li>\n <li><a href=\"//fonts.googleapis.com/css?family=Open+Sans\">Find a Person</a></li>\n <li><a href=\"//fonts.googleapis.com/css?family=Open+Sans\">Find a Person</a></li>\n <li><a href=\"dynamic/city_select.aspx\">Search by city</a></li>\n <li><a href=\"searchbybrand.aspx\">Search by brand</a></li>\n</ul>"

linkList1 = re.findall(re.compile(u'(?<=href=")../.*?(?=")'), str(html))

for link1 in linkList:
html = re.sub(link1, path.scheme + "://" + os.path.normpath(path.netloc + os.path.abspath(lpath + "/" + link1)), str(html))

print (html)


Problem is it detects the links with "../" as intended but also "../../" is changed, is there any way I can restrict my regex to just pick the url's with single "../"?

Expected output:

<ul class="hide menuSearchType">
<li><a href="../../dynamic/city_select.aspx">Search by city</a></li>
<li><a href="../../searchbyphone.aspx">Search by phone</a></li>
<li><a href="http://www.phonebook.com.pk/searchbyaddress.aspx">Search by address</a></li>
<li><a href="http://www.phonebook.com.pk/searchbybrand.aspx">Search by brand</a></li>
<li><a href="/advertisement-center/">Advertise with us</a></li>
<li><a href="/advertisement-center/">Advertise with us</a></li>
<li><a href="//fonts.googleapis.com/css?family=Open+Sans">Find a Person</a></li>
<li><a href="//fonts.googleapis.com/css?family=Open+Sans">Find a Person</a></li>
<li><a href="dynamic/city_select.aspx">Search by city</a></li>
<li><a href="searchbybrand.aspx">Search by brand</a></li>
</ul>

Answer

Try using the following:

linkList1 = re.findall(re.compile(u'(?<=href=")../\w.*?(?=")'), str(html))

That guarantees that there has to be a word character after the slash.

Comments