0

I have a string value that will have following declared value:

<img alt="Desert.jpg" src="/PublishingImages/Lists/Images/NewForm/Desert.jpg" width="174" style="BORDER: 0px solid; ">

The value will have different src value. what I would like to do is to grab the value inside src. I was thinkin of using substring but since the value inside src can varie I dont know how to accomplish this.

This is what I need /PublishingImages/Lists/Images/NewForm/Desert.jpg

Any kind of help is appreciated.

dcastro
  • 66,540
  • 21
  • 145
  • 155
Obsivus
  • 8,231
  • 13
  • 52
  • 97

6 Answers6

2

Use something like Regex.Match(yourString, "src=.*?\"") then do a substring from 5th character and remove the last quote. Regex might not be correct. You should get the idea.

AbhinavRanjan
  • 1,638
  • 17
  • 21
1

Use HtmlAgilityPack to parse HTML:

var doc = new HtmlAgilityPack.HtmlDocument();
doc.Load(@"PathToFile"); // or use doc.LoadHtml to get it from a string
var imgHrefSrc = doc.DocumentNode.SelectSingleNode("//img").Attributes["src"];
string value = imgHrefSrc.Value; // /PublishingImages/Lists/Images/NewForm/Desert.jpg

How to use HTML Agility pack

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

I suggest to use HtmlAgilityPack for parsing HTML (available from NuGet):

string html = "<img alt=\"Desert.jpg\" src=\"/PublishingImages/Lists/Images/NewForm/Desert.jpg\" width=\"174\" style=\"BORDER: 0px solid; \">";
var img = HtmlNode.CreateNode(html);
var src = img.Attributes["src"].Value;

Result:

"/PublishingImages/Lists/Images/NewForm/Desert.jpg"
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

I know you want the answer in c# but here is a java code that parses your string and writes url into console:

public class HelloWorld{

     public static void main(String []args){
       String x= "<img alt=\"Desert.jpg\" src=\"/PublishingImages/Lists/Images/NewForm/Desert.jpg\" width=\"174\" style=\"BORDER: 0px solid; \">";
       String a=x.split(" ")[2].split("\"")[1];

           System.out.println(a);

     }
}
AloneInTheDark
  • 938
  • 4
  • 15
  • 36
0

You can use Regex as the simplest solution that doesn't require additional libraries you may or may not need otherwise:

Regex rx = new Regex("src\\s?=\\s?\"[^\"]*\"");
string x = "<img alt=\"Desert.jpg\" src=\"/PublishingImages/Lists/Images/NewForm/Desert.jpg\" width=\"174\" style=\"BORDER: 0px solid; \">";

Console.WriteLine(rx.IsMatch(x));

Note that this regex allows for whitespace between 'src', '=' and the quoted value and looks for both the opening and closing quote.

Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
0

Try using regex as a lightweight solution.

Try this regex (tested on http://regexpal.com/):

src=\"([^\"]*)\"

And you could extract the string using captured group

Sample code:

string input = "<img alt=\"Desert.jpg\" src=\"/PublishingImages/Lists/Images/NewForm/Desert.jpg\" width=\"174\" style=\"BORDER: 0px solid; \">";
string pattern = "src=\"([^\"]*)\"";
string extractedString = Regex.Match(input, pattern).Groups[1].Value;
Khanh TO
  • 48,509
  • 13
  • 99
  • 115