1

I have the following string

background-image: url('https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg')

and I just want to get the image URL.

My code is:

image = image.Replace(@"'", "\"");
Match match = Regex.Match(image, @"'([^']*)");

Match.Success returns nothing, so I can not get the image URL.

Is there something missing? This used to work but not now.

Apalabrados
  • 1,098
  • 8
  • 21
  • 38
  • 5
    You are replacing “'” with a double-quote `"` and your regex is looking for a single-quote `'`. – Dour High Arch Jul 07 '17 at 18:03
  • I tested in regex hero and dotnet fiddle (https://dotnetfiddle.net/hH1eBE), and I think the answer here: https://stackoverflow.com/a/171499/5758637 will work for you. – Michael Armes Jul 07 '17 at 18:08
  • @DourHighArch Yes!! That was the problem. In some situation is enclosed with " and others with ' – Apalabrados Jul 07 '17 at 18:29
  • if any of the answers solves your problem, please accept the answer to close the thread. – Iqon Jul 07 '17 at 19:04

3 Answers3

1

The following pattern achieves your result, without the usage of string.replace.

var pattern = @"&#39;(?<url>.*)&#39;";
Match match = Regex.Match(image, pattern);
Console.WriteLine($"Math: {match.Groups["url"].Value}");

If you want the " surrounding the string, add this:

var result = $"\"{match.Groups["url"].Value}\""
Iqon
  • 1,920
  • 12
  • 20
1

No need for a regex, just

  • Split the string with &#39; substring
  • Find the element starting with http
  • Return the first found item.

C# demo:

var s = "background-image: url(&#39;https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg&#39;)";
var res = s.Split(new[] {"&#39;"}, StringSplitOptions.None)
    .Where(v => v.StartsWith("http"))
    .FirstOrDefault();
Console.WriteLine(res);
// => https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg 

If you need to use a regex, use the standard regex to match a string between two strings, start(.*?)end where (.*?) captures into Group 1 any 0 or more chars other than a newline, as few as possible as the *? quantifier is lazy:

var s = "background-image: url(&#39;https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg&#39;)";
var res = Regex.Match(s, @"&#39;(.*?)&#39;").Groups[1].Value ?? string.Empty;
Console.WriteLine(res);
// => https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg

See another C# demo

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

The regex of: (\".*\")

Will match the URL given the input string: background-image: url("https://s3-eu-west-1.amazonaws.com/files.domain.com/uploads/image/file/168726/carousel_IMG_6455.jpg")

image = image.Replace(@"&#39;", "\"");
Match match = Regex.Match(image, "(\\\".*\\\")");

Edit: If you are looking for something that will match pairs of single quotes or double quotes you could use:

(\".*\"|'.*')

Match match = Regex.Match(image, "(\\\".*\\\"|'.*')");
Ehz
  • 2,027
  • 1
  • 12
  • 11