Googlesheet正则表达式:仅从多个图片URL中捕获首个URL
If you need to pull the very first image URL from a chunk of HTML (like your example with multiple <a> tags and image attributes), here's a straightforward solution using Google Sheets' REGEXEXTRACT function:
Step 1: The Regex Formula
Use this formula in a blank cell, replacing A1 with the cell holding your HTML content:
=REGEXEXTRACT(A1, "https?://[^"]+")
How It Works
Let’s break down the regex pattern to make sense of it:
https?://: Matches bothhttp://andhttps://URLs (the?makes thesoptional, covering both secure and non-secure links)[^"]+: Matches any character that isn’t a double quote — since your image URLs are wrapped in quotes inside HTML attributes, this ensures we capture the full URL up to the closing quote of the first attribute.
Since REGEXEXTRACT returns the first match it finds by default, this will grab exactly the first image URL from your content.
Optional: Target Multiple Image Formats
If your content includes other image types (like .png, .gif, or .jpeg), tweak the regex to only pull valid image URLs:
=REGEXEXTRACT(A1, "https?://[^"]+\.(jpg|jpeg|png|gif)")
Test with Your Example
For your sample input:
<a data-zoom-image="https://www.example.com/media/catalog/product/cache/1/image/5f444a2891627135a18d90f22b51fc0/d/m/dm104-2.jpg" , data-image="https://www.example.com/media/catalog/product/cache/1/image/500x500/35f444a289162715a18d90f22b51fc0/d/m/dm104-2.jpg"> <a data-zoom-image="https://www.example.com/media/catalog/product/cache/1/image/35f444a28916...
The formula will return:https://www.example.com/media/catalog/product/cache/1/image/5f444a2891627135a18d90f22b51fc0/d/m/dm104-2.jpg
内容的提问来源于stack exchange,提问作者disha

