Poornima Mishra
Poornima Mishra

Reputation: 416

how to get particular tag for html value in swift

I have this set of html

<div class="_57-o acbk" data-store="{&quot;object_id&quot;:254863256822548}" id="u_0_3_XB" data-sigil="photo-stage marea">

 <img src="https://scontent.fdel10-1.fna.fbcdn.net/v/t39.30808-6/274092727_254863253489215_7220866489517235931_n.jpg?stp=cp0_dst-jpg_e15_fr_q65&amp;_nc_cat=107&amp;ccb=1-5&amp;_nc_sid=dd9801&amp;_nc_ohc=EAKHwJL9zzcAX9HXKnn&amp;_nc_ht=scontent.fdel10-1.fna&amp;oh=00_AT-T0elfrrjiMuDTzi2DO2nIS7zzjAjQkeROOj04Lv_v1A&amp;oe=6220A6D4" width="414" height="232" class="img" data-store="{&quot;imgsrc&quot;:&quot;https:\/\/scontent.fdel10-1.fna.fbcdn.net\/v\/t39.30808-6\/274092727_254863253489215_7220866489517235931_n.jpg?stp=cp0_dst-jpg_e15_fr_q65&amp;_nc_cat=107&amp;ccb=1-5&amp;_nc_sid=dd9801&amp;_nc_ohc=EAKHwJL9zzcAX9HXKnn&amp;_nc_ht=scontent.fdel10-1.fna&amp;oh=00_AT-T0elfrrjiMuDTzi2DO2nIS7zzjAjQkeROOj04Lv_v1A&amp;oe=6220A6D4&quot;}" alt="May be an image of 4 people and text" data-sigil="photo-image" data-store-id="0">

how to get the url inside the imgsrc tag

Upvotes: 0

Views: 518

Answers (1)

Rob
Rob

Reputation: 437917

The most robust solution is to use a HTML parser (e.g. Hpple or NDHpple or others).

You can get pretty close just using regular expressions (a.k.a. “regex”). For example, a simple implementation might be:

let regex = /<img.+?src\s*=\s*("|')(?<uri>.*?)\1.*?>/
    .ignoresCase()

for match in string.matches(of: regex) {
    print(match.output.uri)
}

The basic idea is to find the substring within the quotation marks between the <img src=" and the closing ".

But, strictly speaking, a regex approach is not quite correct, as it makes many assumptions about the nature of the HTML. For example, while I am handling the single and double quotation mark scenarios, the IMG tag might not use quotation marks at all. Regex simply is not a replacement for a proper HTML parser. See RegEx match open tags except XHTML self-contained tags.

If this is just a quick-and-dirty exercise, the regex will get you pretty close, but if you really want robust parsing of image URLs, you really should use one of the aforementioned HTML parsers.


For pre iOS 16 solution using legacy regex API, see previous revision of this answer.

Upvotes: 2

Related Questions