How to grab URL from string with specified alt tags

Chris L

I would like to extract "http://www.somewebsite.com/wanted.jpg" from the string below where alt is set to "thumbnail", and avoid grabbing http://www.somewebsite.com/notwanted.jpg :

<span>Some information here
  <div> 
    <img src="http://www.somewebsite.com/notwanted.jpg" width="15" height="15" alt="emoticon"> 
    <img src="http://www.somewebsite.com/wanted.jpg" alt="thumbnail"> 
  </div>
</span>

What is the easiest way to do that?

zx81

With all the warnings about parsing html with regex, this C# regex will match the url you want:

(?<=src=")[^"]+(?="[^">]*?alt="thumbnail")

See demo.

To test it in C#:

var myRegex = new Regex("(?<=src=\")[^\"]+(?=\"[^\">]*?alt=\"thumbnail\")");
string resultString = myRegex.Match(s1).Value;
Console.WriteLine(resultString);

Output:

http://www.somewebsite.com/wanted.jpg

Explanation

  • The lookbehind (?<=src=") asserts that what precedes is src="
  • [^"]+ matches all chars that are not a " (that's what we want)
  • The lookahead (?="[^">]*?alt="thumbnail")asserts that what follows is a quote, and any chars that are not a quote or a > followed by `alt="thumbnail"

Reference

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to grab URL from string with specified alt tags

From Dev

String function to grab specified character

From Dev

SafariViewController: How to grab OAuth token from URL?

From Dev

How to grab text from a messy string in java?

From Dev

Can't grab string from URL with special characters

From Dev

How to get the alt value from this string?

From Dev

Remove tags and url from img string

From Dev

How to Grab a String from This to That from a Webpage's Source?

From Dev

How to grab Image url and title from elements above in same article

From Dev

how to post a photo from android to the cloud and grab its url alter?

From Dev

Grab the href and text of <a> tags from the page

From Dev

How to grab ObjectId from object in parse and present it as string in swift

From Dev

Regex from a html parsing, how do I grab a specific string?

From Dev

How can I grab a group of words from a string?

From Dev

How to grab values from unformatted string in ios objective-c?

From Dev

How to Retrieve query string parameters from url specified in ng-include AngularJs

From Dev

Fancy box, grab from url

From Dev

Grab DIV Hash From URL

From Dev

Grab #id from the URL in jQuery

From Dev

Grab Amount from Specific String

From Dev

grab just the cents from string

From Dev

grab just the cents from string

From Dev

how to grab alternating child tags in python beautifulsoup

From Dev

How to remove all html tags from a string

From Dev

How to delete tags including text from string

From Dev

OpenEdge: how to remove HTML tags from a string?

From Dev

How to replace urls from a string into an anchor tags

From Dev

How to check a specified string is a valid URL or not using C++ code

From Dev

How to check a specified string is a valid URL or not using C++ code

Related Related

  1. 1

    How to grab URL from string with specified alt tags

  2. 2

    String function to grab specified character

  3. 3

    SafariViewController: How to grab OAuth token from URL?

  4. 4

    How to grab text from a messy string in java?

  5. 5

    Can't grab string from URL with special characters

  6. 6

    How to get the alt value from this string?

  7. 7

    Remove tags and url from img string

  8. 8

    How to Grab a String from This to That from a Webpage's Source?

  9. 9

    How to grab Image url and title from elements above in same article

  10. 10

    how to post a photo from android to the cloud and grab its url alter?

  11. 11

    Grab the href and text of <a> tags from the page

  12. 12

    How to grab ObjectId from object in parse and present it as string in swift

  13. 13

    Regex from a html parsing, how do I grab a specific string?

  14. 14

    How can I grab a group of words from a string?

  15. 15

    How to grab values from unformatted string in ios objective-c?

  16. 16

    How to Retrieve query string parameters from url specified in ng-include AngularJs

  17. 17

    Fancy box, grab from url

  18. 18

    Grab DIV Hash From URL

  19. 19

    Grab #id from the URL in jQuery

  20. 20

    Grab Amount from Specific String

  21. 21

    grab just the cents from string

  22. 22

    grab just the cents from string

  23. 23

    how to grab alternating child tags in python beautifulsoup

  24. 24

    How to remove all html tags from a string

  25. 25

    How to delete tags including text from string

  26. 26

    OpenEdge: how to remove HTML tags from a string?

  27. 27

    How to replace urls from a string into an anchor tags

  28. 28

    How to check a specified string is a valid URL or not using C++ code

  29. 29

    How to check a specified string is a valid URL or not using C++ code

HotTag

Archive