Python regular expression returns nothing for match
<p>I am learning web scrapping using Python. I am trying to extract all links from one of popular financial site's site map.</p>

<pre><code>bsObj = BeautifulSoup(html, "html.parser")

for link in bsObj.findAll("a", 
href=re.compile("^(/india/stockmarket/pricechartquote/)*$")):

    if 'href' in link.attrs:
        print(link.attrs['href'])
print('found nothing')
</code></pre>

<p>This code founds nothing. Although many links with above match is present in site.
Sample : /india/stockmarket/pricechartquote/A</p>

<p>Have you tried checking if this regex matches the provided part of a url - it does not:</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt;
&gt;&gt;&gt; pattern = re.compile("^(/india/stockmarket/pricechartquote/)*$")
&gt;&gt;&gt; pattern.search("/india/stockmarket/pricechartquote/A")
&gt;&gt;&gt;
</code></pre>

<p>Instead, you meant to have the last part after the <code>pricechartquote/</code> matching, for instance, one or more uppercase letters: </p>

<pre><code>&gt;&gt;&gt; pattern = re.compile(r"^/india/stockmarket/pricechartquote/[A-Z]+$")
&gt;&gt;&gt; pattern.search("/india/stockmarket/pricechartquote/A")
&lt;_sre.SRE_Match object at 0x109240098&gt;
</code></pre>

<p>Please adjust the <code>[A-Z]+</code> part depending on what kind of character set you expect to see after <code>pricechartquote/</code>.</p>

<hr>

<p>Also note that you don't have to check the beginning and end of the string and might be good to go with a partial url match:</p>

<pre><code>for link in bsObj.find_all("a", href=re.compile(r"/india/stockmarket/pricechartquote/")):
    # ...
</code></pre>

python regex beautifulsoup