Extracting original product image url from filter applied url

Shopify product image URLs used in stores have URL filters applied to them as shown below

https://cdn.shopify.com/s/files/1/0271/358/products/abc_def_1600x.jpg?v=1648058142

From this URL, I was wondering if there is any standard logic prescribed by Shopify to extract the original product URL which is

https://cdn.shopify.com/s/files/1/0271/358/products/abc_def.jpg?v=1648058142

My initial approach was this

  1. Extract the file name from the URL(the part between the last / and the ?)
  2. Remove everything between the first underscore and the extension

This approach worked when the filename did not have underscores. But it will fail in cases like above where the filename itself has underscores. Is there any definitive way that will work for all filenames?

Can you post the current code you have so far?