Search code examples
pythonpython-3.xsplitstartswithends-with

Python: How i Can print only a particular string starting with a specific character?


I have a text file which contains list of urls which looks like as follows:

https://www.ebay.com/itm/Egyptian-Comfort-1800-Count-4-Piece-Bed-Sheet-Set-Deep-Pocket-Bed-Sheets/142436469971?epid=1760442729&hash=item2129e00cd3%3Ag%3A7gIAAOSw3YBdRVJd&_trkparms=%2526rpp_cid%253D601435485fceeb223c6f4511&var=442541824291

Here i only want to print epid=1760442729 while reading the text file.

I have tried:

result = []`
with open('deals.txt', 'r') as f:
for line in f:
    if line.startswith('?epid='):
        break
    result.append(line)
print(result[0].split('epid='))

But i am not getting as expected result.

Any help or suggestions will be helpful for me. Thanks in advance


Solution

  • import re
    
    s = """https://www.ebay.com/itm/Egyptian-Comfort-1800-Count-4-Piece-Bed-Sheet-Set-Deep-Pocket-Bed-Sheets/142436469971?epid=1760442729&hash=item2129e00cd3%3Ag%3A7gIAAOSw3YBdRVJd&_trkparms=%2526rpp_cid%253D601435485fceeb223c6f4511&var=442541824291
    https://www.ebay.com/itm/Egyptian-Comfort-1800-Count-4-Piece-Bed-Sheet-Set-Deep-Pocket-Bed-Sheets/142436469971?epid=172442729&hash=item2129e00cd3%3Ag%3A7gIAAOSw3YBdRVJd&_trkparms=%2526rpp_cid%253D601435485fceeb223c6f4511&var=442541824291"""
    
    for i in re.findall(r'epid=(\d+)&', s, re.MULTILINE):
        print(f'epid = {i}')
    
    epid = 1760442729
    epid = 172442729