Search code examples
pythonregexstringregex-negationregex-greedy

RegEx for matching URLs and failing non-URLs


I am trying to extract separated urls from srcset of an image.

This is for extracting and downloading all the files present in the row. I am not hard coding it because row can differ from time to time.

txt = "images/1x/1083bce6__color_272x92dp.png 1x, images/2x/b3716cd2_color_272x92dp.png 2x"

reg = re.compile(r"((?:[^\s,]+))(?:.*?,)?").finditer

for i in list(reg(txt)):
   print(i.group(1))


I expect the result to be two urls

images/1x/1083bce6__color_272x92dp.png
images/2x/b3716cd2_color_272x92dp.png

But I get three with extra being

2x


Solution

  • Here, we would add a quantifier to a simple char class in an expression such as:

    [^\s]{4,}
    

    to get our desired URLs.

    Demo

    Test

    # coding=utf8
    # the above tag defines encoding for this document and is for Python 2.x compatibility
    
    import re
    
    regex = r"[^\s]{4,}"
    
    test_str = "images/1x/1083bce6__color_272x92dp.png 1x, images/2x/b3716cd2_color_272x92dp.png 2x"
    
    matches = re.finditer(regex, test_str, re.MULTILINE)
    
    for matchNum, match in enumerate(matches, start=1):
        
        print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
        
        for groupNum in range(0, len(match.groups())):
            groupNum = groupNum + 1
            
            print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
    
    # Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
    

    RegEx

    If this expression wasn't desired, it can be modified/changed in regex101.com.

    RegEx Circuit

    jex.im visualizes regular expressions:

    enter image description here